UIImagePickerController保存到磁盘然后加载到UIImageView

时间:2010-03-12 16:54:35

标签: iphone uiimageview uiimagepickercontroller

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

这是我的代码:

- (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中。谁能告诉我这里有什么问题?

非常感谢。

3 个答案:

答案 0 :(得分:7)

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

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

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

答案 1 :(得分:1)

使用NSDateFormatterShortStyle将导致日期表示为(例如)12/11/12,这将弄乱您的文件名。它也会在短时间内使用冒号,这是非法的。

我建议使用更自定义的日期格式,例如:

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"yyyyMMdd-HHmmss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

答案 2 :(得分:0)

我在我的一个测试项目中使用了这段代码并模拟了这个bug。 Jordan在将其再次更改为UIImage之前首先将其转换为NSData,但真正的问题在于您的文件命名方法。

我注意到您已使用当前日期和时间作为图片的文件名。这包括诸如“:”和“/”之类的字符,不允许在文件名上使用。 http://www.portfoliofaq.com/pfaq/FAQ00352.htm

作为解决方案,我将日期格式设为如下所示:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyyMddHHmm"]; NSString *imageName = [NSString stringWithFormat:@"photo-%@.png", [dateFormatter stringFromDate:[NSDate date]]];

希望这对其他人也有帮助。 :)