2010-03-12 44 views
2

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

这里是我的代码:

- (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中。任何人都可以告诉我我在这里有什么不对吗?

非常感谢。

+0

尝试不保存完整路径,只显示图像名称并在显示前查找路径。 – RunLoop 2010-03-13 03:45:18

回答

7

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

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

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

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

我会建议使用多个自定义日期格式,如:

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]]; 
0

我用我的测试项目之一,该代码和模拟的bug。乔丹在将它转换为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]]];

希望这有助于其他人。 :)