【发布时间】:2015-10-20 09:19:26
【问题描述】:
我几天以来一直在尝试解决这个问题。我有一个应用程序,用户选择一个图像,然后将所选图像保存到磁盘并稍后加载到 UIImageView 中,但是无论我尝试什么,这都不起作用。我制作了一个小应用程序只是为了测试它,它在那里也不起作用。 imageView 保持空白。这是我的代码:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)button:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *selectedImage = info[UIImagePickerControllerOriginalImage];
[self saveImage:selectedImage];
self.imageView.image = [self loadImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)saveImage: (UIImage*)image
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
@"image.png" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
@"image.png" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
@end
我做错了什么?
【问题讨论】:
标签: ios uiimageview uiimage save load