保存的图像不显示 [英] Image Being Saved is Not Appearing

查看:102
本文介绍了保存的图像不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Core Data保存我的照片,但是当我拍摄照片并返回到 DetailViewController 时,在 UIImageView 。让我解释一下发生了什么:
用户按下添加配置文件图片按钮,提示他们在照片被拍摄后照相,它显示照片的预览,用户可以重新拍摄照片或保存图像。当他们推保存图像时,ViewController被关闭,当我推存储并返回到这个视图控制器时, UIImageView 中没有看到任何照片在 UIImageView

I am trying to save my photo using Core Data, however, when I take the photo and return to the DetailViewControllernothing is seen in the UIImageView. Let me explain exactly what happens: The user pushes the "Add A Profile Picture" button which prompts them to take a photo after the photo is taken as normal it shows the preview of the photo where the user can either retake the photo or save the image. When they push save image the ViewController is dismissed and no photo is seen in the UIImageView when I push save and go back into this view controller nothing is still seen in the UIImageView.

我已在下面包含我的代码。

I have included my code below.

-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.                                                                                                                                                                                   

if (self.device) {
    [self.scoutNameText setText:[self.device valueForKey:@"name"]];
    [self.scoutContactText setText:[self.device valueForKey:@"number"]];
    [self.scoutTeamText setText:[self.device valueForKey:@"team"]];
    [self.email setText:[self.device valueForKey:@"email"]];
    [self.pick1 setText:[self.device valueForKey:@"pick1"]];
    [self.pick2 setText:[self.device valueForKey:@"pick2"]];
    [self.pick3 setText:[self.device valueForKey:@"pick3"]];
    [self.pick4 setText:[self.device valueForKey:@"pick4"]];
    [self.pick5 setText:[self.device valueForKey:@"pick5"]];
    [self.pick6 setText:[self.device valueForKey:@"pick6"]];
    [self.jobtitle setText:[self.device valueForKey:@"jobtitle"]];



}

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"scout.png"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
[self.photo setImage:[UIImage imageWithData:data]];

}

- (IBAction)ProfilePhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:picker animated:YES completion:nil];
}
else {
    [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [picker setDelegate:self];
    [self presentViewController:picker animated:YES completion:nil];
}

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

NSData *imageData = UIImagePNGRepresentation(image);

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"scout.png"];
[imageData writeToFile:filePath atomically:YES];
[self.photo setImage:[UIImage imageWithData:imageData]];


[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (IBAction)save:(id)sender {

// Update existing device
NSManagedObjectContext *context = [self managedObjectContext];
[self.device setValue:self.scoutNameText.text forKey:@"name"];
[self.device setValue:self.scoutContactText.text forKey:@"number"];
[self.device setValue:self.scoutTeamText.text forKey:@"team"];
[self.device setValue:self.pick1.text forKey:@"pick1"];
[self.device setValue:self.pick2.text forKey:@"pick2"];
[self.device setValue:self.pick3.text forKey:@"pick3"];
[self.device setValue:self.pick4.text forKey:@"pick4"];
[self.device setValue:self.pick5.text forKey:@"pick5"];
[self.device setValue:self.pick6.text forKey:@"pick6"];
[self.device setValue:self.email.text forKey:@"email"];
[self.device setValue:self.jobtitle.text forKey:@"jobtitle"];




 NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}



[self dismissViewControllerAnimated:YES completion:nil];

}

感谢您花时间看看我的问题,帮我!非常感谢!

Thank you for taking the time to look at my question and help me! Many Thanks!

推荐答案

您尝试将图像保存在 NSDocumentationDirectory (Documentation),
默认不存在,因此

You (try to) save the image in the NSDocumentationDirectory ("Documentation"), which does not exist by default, so that

[imageData writeToFile:filePath atomically:YES];

失败,

NSData *data = [NSData dataWithContentsOfFile:filePath];

返回 nil 。您可能希望将图像保存在 NSDocumentDirectory
(Documents)中。

returns nil. You probably want to save the image in the NSDocumentDirectory ("Documents") instead.

,我建议总是检查调用的返回值,如

Generally, I would recommend to always check the return value of calls like

BOOL success = [imageData writeToFile:filePath atomically:YES];

可检测此类错误。

除此之外,我不能在你的代码中看到图像(或图像路径)存储
在托管对象 self.device 在任何地方,这意味着它在Core Data中根本不存储

Apart from that, I cannot see in your code that the image (or image path) is stored in the managed object self.device anywhere, which means that it is not stored in Core Data at all.

这篇关于保存的图像不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆