2011-10-20 25 views
32

我使用下面的代碼保存和載入,我無論從庫中挑選圖片或使用相機拍攝:保存的UIImage,加載它在錯誤的方向

//saving an image 
- (void)saveImage:(UIImage*)image:(NSString*)imageName { 
    NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format. 
    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it 
    NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path 
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image) 
    NSLog(@"image saved"); 
} 

//loading an image 
- (UIImage*)loadImage:(NSString*)imageName { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; 
    return [UIImage imageWithContentsOfFile:fullPath]; 
} 

我這是怎麼設置的拍攝圖像要顯示在我UIImageView

imgView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

然而,當我拿起和形象,並設置它在我UIImageView要顯示它是好的,但是當我加載圖像往往是錯誤的方向。有任何想法嗎?任何人都經歷過這個或知道我該如何解決這個問題

謝謝。

編輯:

如此看來,如果你加載的拍攝人像照片顛倒,它加載在該方向,如果你把在景觀中的照片留下它加載在該方向的照片。任何想法如何解決這個問題?無論他們加載的方向如何,它們總是以UIImageOrientationUp的形式返回。

+0

只需查看UIImage文檔的UIImageOrientationUp即可。你可以設置UIImage對象的imageOrientation屬性。 –

+0

但我怎樣才能應用這個方向?我看了一下,它似乎只是爲了比較,而且輪換必須手動完成。 –

+0

請檢查我的編輯。 –

回答

25

我都面臨着類似的問題,這是我如何解決它。

雖然我們保存圖像,我們需要保存其方位信息與圖像一起...

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
[userDefaults setInteger:[myImage imageOrientation] forKey:@"kImageOrientation"]; 
[imageOrientation release]; 

我們加載圖像,我們需要讀取它的方位信息,並將其應用到圖像...

UIImage *tempImage = [[UIImage alloc] initWithContentsOfFile:fullPath]; 
UIImage *orientedImage= [[UIImage alloc] initWithCGImage: tempImage.CGImage scale:1.0 orientation:imageOrientation]; 
[tempImage release]; 

orientedImage是WH在我們需要。

感謝,

+0

即使UIImage已被轉換爲NSData並再次返回,這是否工作? – zakdances

+0

謝謝,當我終於找到這個答案時,在6個小時內出現了奇怪的旋轉問題。 –

+1

這解決了我的問題,但只有一次我開始使用ALAsset方向屬性而不是UIImage方向屬性。 –

0

嘗試保存圖像方向,然後當你加載圖像,檢查是否是正確的方向,如果沒有,用CGAffineTransform或任何你想要的旋轉圖像。

3

檢查您正在加載的圖像的EXIF信息,應該有一個方向標籤。使用該值將圖像旋轉到正確的方向。

這個網站應該讓你開始。

http://www.impulseadventure.com/photo/exif-orientation.html

(您用來查看照片必須有這個內置的應用程序,這就是爲什麼你看到正確的方向打開照片時)

+1

啊,非常有趣,但是我將如何去獲取exif方向數據呢? –

47

的根本原因是PNG格式沒有形象定位信息,但JPEG格式一樣。因此,解決此問題的最簡單方法是使用UIImageJPEGRepresentation()

+0

這解決了我的問題,保存的圖像沒有以正確的方向返回 - 尤其是從相機拍攝的圖像。使用JPEG替代品的唯一缺點是透明度不被保留,是否正確? – micnguyen

1

將文件保存爲JPEG格式。您可以使用Image I/O將PNG圖像保存爲文件(或NSMutableData)與原始圖像的方向有關。在下面的示例中,我將PNG圖像保存爲path的文件。

- (BOOL)savePngFile:(UIImage *)image toPath:(NSString *)path { 
    NSData *data = UIImagePNGRepresentation(image); 
    int exifOrientation = [UIImage cc_iOSOrientationToExifOrientation:image.imageOrientation]; 
    NSDictionary *metadata = @{(__bridge id)kCGImagePropertyOrientation:@(exifOrientation)}; 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); 
    if (!source) { 
     return NO; 
    } 
    CFStringRef UTI = CGImageSourceGetType(source); 
    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, UTI, 1, NULL); 
    if (!destination) { 
     CFRelease(source); 
     return NO; 
    } 
    CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metadata); 
    BOOL success = CGImageDestinationFinalize(destination); 
    CFRelease(destination); 
    CFRelease(source); 
    return success; 
} 

cc_iOSOrientationToExifOrientation:UIImage類別的方法。

+ (int)cc_iOSOrientationToExifOrientation:(UIImageOrientation)iOSOrientation { 
    int exifOrientation = -1; 
    switch (iOSOrientation) { 
     case UIImageOrientationUp: 
      exifOrientation = 1; 
      break; 

     case UIImageOrientationDown: 
      exifOrientation = 3; 
      break; 

     case UIImageOrientationLeft: 
      exifOrientation = 8; 
      break; 

     case UIImageOrientationRight: 
      exifOrientation = 6; 
      break; 

     case UIImageOrientationUpMirrored: 
      exifOrientation = 2; 
      break; 

     case UIImageOrientationDownMirrored: 
      exifOrientation = 4; 
      break; 

     case UIImageOrientationLeftMirrored: 
      exifOrientation = 5; 
      break; 

     case UIImageOrientationRightMirrored: 
      exifOrientation = 7; 
      break; 

     default: 
      exifOrientation = -1; 
    } 
    return exifOrientation; 
} 

您也可以使用CGImageDestinationCreateWithData將圖像保存到NSMutableDataCGImageDestinationCreateWithURL通過NSMutableData而不是NSURL