2011-02-14 45 views
10

我使用UIImagePickerController來捕獲圖像,然後將其存儲。但是,當我嘗試重新縮放它時,我從該圖像中獲取的方向值不正確。當我通過拿起手機拍下來時,它給了我左側的方向。有沒有人遇到過這個問題?UIImagePickerController返回不正確的圖像方向

的字典的UIImagePickerController顯示以下信息:

{ 
    UIImagePickerControllerMediaMetadata =  { 
     DPIHeight = 72; 
     DPIWidth = 72; 
     Orientation = 3; 
     "{Exif}" =   { 
      ApertureValue = "2.970853654340484"; 
      ColorSpace = 1; 
      DateTimeDigitized = "2011:02:14 10:26:17"; 
      DateTimeOriginal = "2011:02:14 10:26:17"; 
      ExposureMode = 0; 
      ExposureProgram = 2; 
      ExposureTime = "0.06666666666666667"; 
      FNumber = "2.8"; 
      Flash = 32; 
      FocalLength = "3.85"; 
      ISOSpeedRatings =    (
       125 
      ); 
      MeteringMode = 1; 
      PixelXDimension = 2048; 
      PixelYDimension = 1536; 
      SceneType = 1; 
      SensingMethod = 2; 
      Sharpness = 1; 
      ShutterSpeedValue = "3.910431673351467"; 
      SubjectArea =    (
       1023, 
       767, 
       614, 
       614 
      ); 
      WhiteBalance = 0; 
     }; 
     "{TIFF}" =   { 
      DateTime = "2011:02:14 10:26:17"; 
      Make = Apple; 
      Model = "iPhone 3GS"; 
      Software = "4.2.1"; 
      XResolution = 72; 
      YResolution = 72; 
     }; 
    }; 
    UIImagePickerControllerMediaType = "public.image"; 
    UIImagePickerControllerOriginalImage = "<UIImage: 0x40efb50>"; 
} 

然而圖片返回imageOrientation == 1;

UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];    

回答

14

我剛開始在我自己的應用程序中處理這個問題。

我使用Trevor Harmon製作的UIImage類別來調整圖像大小並固定其方向UIImage+Resize

然後,你可以做這樣的事情在-imagePickerController:didFinishPickingMediaWithInfo:

 
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerEditedImage]; 
UIImage *resized = [pickedImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:pickedImage.size interpolationQuality:kCGInterpolationHigh]; 

這解決了這個問題對我來說。調整大小的圖像的視覺方向正確,imageOrientation屬性報告UIImageOrientationUp。

這裏有幾種版本的縮放/縮放/裁剪代碼;我使用Trevor的是因爲它看起來很乾淨,並且包含了一些其他的UIImage操作符,我稍後會使用它們。

+0

這看起來不符合ARC兼容代碼。它失敗並顯示錯誤:「'UIImage'沒有可見的@interface聲明選擇器'resizedImageWithContentMode:bounds:interpolationQuality:'」 – mpemburn 2012-10-10 17:17:16

+0

您可能會看到由Marc Charbonneau在GitHub上維護的此代碼的更新分支。 2012-10-10 17:30:30

8

這是我找到的固定方向問題;對我的作品

UIImage *initialImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
NSData *data = UIImagePNGRepresentation(self.initialImage); 

UIImage *tempImage = [UIImage imageWithData:data]; 
UIImage *fixedOrientationImage = [UIImage imageWithCGImage:tempImage.CGImage 
            scale:initialImage.scale 
           orientation:self.initialImage.imageOrientation]; 
initialImage = fixedOrientationImage; 
0

我用下面的代碼,我已經把具有用於UIImages一堆其他的編輯方法單獨的圖像工具對象文件:

+ (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSizeWithSameAspectRatio:(CGSize)targetSize 
{ 
    CGSize imageSize = sourceImage.size; 
    CGFloat width = imageSize.width; 
    CGFloat height = imageSize.height; 
    CGFloat targetWidth = targetSize.width; 
    CGFloat targetHeight = targetSize.height; 
    CGFloat scaleFactor = 0.0; 
    CGFloat scaledWidth = targetWidth; 
    CGFloat scaledHeight = targetHeight; 
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) { 
     CGFloat widthFactor = targetWidth/width; 
     CGFloat heightFactor = targetHeight/height; 

     if (widthFactor > heightFactor) { 
      scaleFactor = widthFactor; // scale to fit height 
     } 
     else { 
      scaleFactor = heightFactor; // scale to fit width 
     } 

     scaledWidth = width * scaleFactor; 
     scaledHeight = height * scaleFactor; 

     // center the image 
     if (widthFactor > heightFactor) { 
      thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
     } 
     else if (widthFactor < heightFactor) { 
      thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
     } 
    } 

    CGImageRef imageRef = [sourceImage CGImage]; 
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); 

    if (bitmapInfo == kCGImageAlphaNone) { 
     bitmapInfo = kCGImageAlphaNoneSkipLast; 
    } 

    CGContextRef bitmap; 

    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) { 
     bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

    } else { 
     bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

    } 

    // In the right or left cases, we need to switch scaledWidth and scaledHeight, 
    // and also the thumbnail point 
    if (sourceImage.imageOrientation == UIImageOrientationLeft) { 
     thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x); 
     CGFloat oldScaledWidth = scaledWidth; 
     scaledWidth = scaledHeight; 
     scaledHeight = oldScaledWidth; 

     CGContextRotateCTM (bitmap, M_PI_2); // + 90 degrees 
     CGContextTranslateCTM (bitmap, 0, -targetHeight); 

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) { 
     thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x); 
     CGFloat oldScaledWidth = scaledWidth; 
     scaledWidth = scaledHeight; 
     scaledHeight = oldScaledWidth; 

     CGContextRotateCTM (bitmap, -M_PI_2); // - 90 degrees 
     CGContextTranslateCTM (bitmap, -targetWidth, 0); 

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) { 
     // NOTHING 
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) { 
     CGContextTranslateCTM (bitmap, targetWidth, targetHeight); 
     CGContextRotateCTM (bitmap, -M_PI); // - 180 degrees 
    } 

    CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), imageRef); 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage* newImage = [UIImage imageWithCGImage:ref]; 

    CGContextRelease(bitmap); 
    CGImageRelease(ref); 

    return newImage; 
} 

然後我打電話

UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     UIImage *fixedOriginal = [ImageUtil imageWithImage:[mediaInfoDict objectForKey:UIImagePickerControllerOriginalImage] scaledToSizeWithSameAspectRatio:pickedImage.size]; 
4

這裏有一個雨燕片段,有效地解決了這個問題:

let orientedImage = UIImage(CGImage: initialImage.CGImage, scale: 1, orientation: initialImage.imageOrientation)! 
0

在iOS 7中,我需要依賴於UIImage.imageOrientation的代碼來糾正不同的方向。現在,在iOS 8.2中,當我通過UIImagePickerController從相冊中選取舊測試圖像時,所有圖像的方向將爲UIImageOrientationUp。當我拍攝照片(UIImagePickerControllerSourceTypeCamera)時,無論設備方向如何,這些圖像也總是向上。 因此,在這些iOS版本之間,顯然有一個修復,其中UIImagePickerController已經旋轉圖像,如果必要的話。 您甚至可以注意到,當相冊圖像顯示時:在瞬間,它們將以原始方向顯示,然後以新的向上方向出現。

相關問題