UIImagePickerController保存到磁盘然后加载到UIImageView [英] UIImagePickerController Save to Disk then Load to UIImageView

查看:123
本文介绍了UIImagePickerController保存到磁盘然后加载到UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIImagePickerController,它将图像作为png保存到磁盘。当我尝试加载PNG并将UIImageView的 imageView.image 设置为该文件时,它不会显示。

I have a UIImagePickerController that saves the image to disk as a png. When I try to load the PNG and set a UIImageView's imageView.image to the file, it is not displaying.

这是我的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSData *imageData = UIImagePNGRepresentation(image);

    // Create a file name for the image
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    NSString *imageName = [NSString stringWithFormat:@"photo-%@.png",
                           [dateFormatter stringFromDate:[NSDate date]]];
    [dateFormatter release];

    // Find the path to the documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    // Now we get the full path to the file
    NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

    // Write out the data.
    [imageData writeToFile:fullPathToFile atomically:NO];

    // Set the managedObject's imageLocation attribute and save the managed object context
    [self.managedObject setValue:fullPathToFile forKey:@"imageLocation"];
    NSError *error = nil;
    [[self.managedObject managedObjectContext] save:&error];


    [self dismissModalViewControllerAnimated:YES];
}

然后我会尝试加载它:

self.imageView.backgroundColor = [UIColor lightGrayColor];
self.imageView.frame = CGRectMake(10, 10, 72, 72);
if ([self.managedObject valueForKey:@"imageLocation"] != nil) {
    NSLog(@"Trying to load the imageView with: %@", [self.managedObject valueForKey:@"imageLocation"]);
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self.managedObject valueForKey:@"imageLocation"]];
    self.imageView.image = image;

} else {
    self.imageView.image = [UIImage imageNamed:@"no_picture_taken.png"];
}

我收到的消息是它试图在调试器中加载imageView,但是图像永远不会显示在imageView中。谁能告诉我这里有什么问题?

I get the message that it's trying to load the imageView in the debugger, but the image is never displayed in the imageView. Can anyone tell me what I've got wrong here?

非常感谢。

推荐答案

你正在写出一个NSData对象,而不是一个图像。你需要重新读取NSData对象并转换为UIImage。

You're writing out an NSData object, not an image. You need to read the NSData object back in and convert to UIImage.

假设其他一切都正确,试试这个:

Assuming everything else is correct, try this:

NSData *data = [[NSData alloc] initWithContentsOfFile:[self.managedObject valueForKey:@"imageLocation"]];
UIImage *image = [[UIImage alloc] initWithData:data];

这篇关于UIImagePickerController保存到磁盘然后加载到UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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