我正在尝试使用Core Data保存我的照片,但是,当我拍摄照片并返回DetailViewController
时,UIImageView
中看不到任何内容。让我解释一下到底发生了什么:
用户按下“添加个人资料图片”按钮,提示他们在正常拍摄照片后拍照,它显示照片的预览,用户可以重新拍摄照片或保存图像。当他们按下保存图像时,ViewController被解除,当我按下保存并且回到此视图控制器时,UIImageView
中没有看到任何照片。UIImageView
中仍然看不到任何内容。
我在下面提供了我的代码。
-(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];
}
感谢您抽出宝贵时间来查看我的问题并帮助我!非常感谢!
答案 0 :(得分:0)
您(尝试)将图像保存在NSDocumentationDirectory
(“文档”)中,
默认情况下不存在,所以
[imageData writeToFile:filePath atomically:YES];
失败并且
NSData *data = [NSData dataWithContentsOfFile:filePath];
返回nil
。您可能希望将图像保存在NSDocumentDirectory
中
(“文件”)代替。
一般来说,我建议总是检查调用的返回值,如
BOOL success = [imageData writeToFile:filePath atomically:YES];
检测这样的错误。
除此之外,我在代码中看不到存储图像(或图像路径)
在托管对象self.device
中的任何位置,这意味着它不存储
在核心数据中。