2010-03-12 42 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]]];

希望這有助於其他人。 :)