我可以将照片从相机胶卷上传到 ui 图像查看器,但是当我重新启动应用程序或转到另一个页面并返回时,上传的照片消失了。如何保留它们?
- (IBAction)selectImage:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo
{
UIImage *chosenImage = selectedImage;
self.homeImage.image = chosenImage;
[self.view setNeedsDisplay];
[picker dismissViewControllerAnimated:YES completion:NULL];
NSData *imageData = UIImagePNGRepresentation(chosenImage.images);
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/myImage.png"];
[imageData writeToFile:imagePath atomically:YES];
}
我希望第二种方法是将图像保存在内部数据库中并继续显示我上传的图像,但事实并非如此。
您需要从文件加载图像并在创建视图后将其分配给视图,类似于:
- (void)viewDidLoad {
NSString * imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/myImage.png"];
self.homeImage.image = [UIImage imageWithContentsOfFile:imagePath];
}