2012-03-18 4 views
7

iPadフォトライブラリから写真をいくつか取得しています。私は風景モードでいくつかのテスト写真を撮りました。左側のiPadのホームボタンと右側の両方にあります。imageOrientationがUpを返しますが、写真がupsidedownで表示されます

私のアプリでは、何らかの理由で写真の一部がアップサイドダウンを表示しています。私は彼らのimageOrientationプロパティをチェックし、それらはすべて0です(つまり、それらはUIImageOrientationUpです)。つまり、どの写真を回転する必要があるのか​​わからなくても意味があります。

何が起こっているのですか、どの写真を回転する必要があるのか​​を教えてください。ありがとう

+0

見るこのhttps://github.com/cosnovae/fixUIImageOrientationます。また、[カメラロールにプレビューで90度AVFoundationイメージの向きオフが、細かい]同様のポストを参照することができます –

+0

( http://stackoverflow.com/questions/15956750/avfoundation-image-orientation-off-by-90-degrees-in-the-preview-but-fine-in-came/16074603#16074603) –

答えて

4

UIImageにはimageIIrientationというプロパティがあり、これはUIImageViewや他のUIImageのコンシューマに生画像データをローテーションするように指示します。このフラグがアップロードされたJPEG画像のexifデータに保存されている可能性は十分ありますが、それを表示するために使用するプログラムはそのフラグを尊重していません。アップロードされたとき、あなたはこのようにカテゴリを使用することができ、適切に表示するためにUIImageを回転させるように

: .hファイルで

UIImagefixOrientation.h

@interface UIImage (fixOrientation) 

- (UIImage *)fixOrientation; 

@end 

UIImagefixOrientation.m

@implementation UIImage (fixOrientation) 

- (UIImage *)fixOrientation { 

    // No-op if the orientation is already correct 
    if (self.imageOrientation == UIImageOrientationUp) return self; 

    // We need to calculate the proper transformation to make the image upright. 
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. 
    CGAffineTransform transform = CGAffineTransformIdentity; 

    switch (self.imageOrientation) { 
     case UIImageOrientationDown: 
     case UIImageOrientationDownMirrored: 
     transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height); 
     transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

    case UIImageOrientationLeft: 
    case UIImageOrientationLeftMirrored: 
     transform = CGAffineTransformTranslate(transform, self.size.width, 0); 
     transform = CGAffineTransformRotate(transform, M_PI_2); 
     break; 

    case UIImageOrientationRight: 
    case UIImageOrientationRightMirrored: 
     transform = CGAffineTransformTranslate(transform, 0, self.size.height); 
     transform = CGAffineTransformRotate(transform, -M_PI_2); 
     break; 
} 

switch (self.imageOrientation) { 
    case UIImageOrientationUpMirrored: 
    case UIImageOrientationDownMirrored: 
     transform = CGAffineTransformTranslate(transform, self.size.width, 0); 
     transform = CGAffineTransformScale(transform, -1, 1); 
     break; 

    case UIImageOrientationLeftMirrored: 
    case UIImageOrientationRightMirrored: 
     transform = CGAffineTransformTranslate(transform, self.size.height, 0); 
     transform = CGAffineTransformScale(transform, -1, 1); 
     break; 
} 

// Now we draw the underlying CGImage into a new context, applying the transform 
// calculated above. 
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, 
             CGImageGetBitsPerComponent(self.CGImage), 0, 
             CGImageGetColorSpace(self.CGImage), 
             CGImageGetBitmapInfo(self.CGImage)); 
CGContextConcatCTM(ctx, transform); 
switch (self.imageOrientation) { 
    case UIImageOrientationLeft: 
    case UIImageOrientationLeftMirrored: 
    case UIImageOrientationRight: 
    case UIImageOrientationRightMirrored: 
     // Grr... 
     CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); 
     break; 

    default: 
     CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage); 
     break; 
} 

// And now we just create a new UIImage from the drawing context 
CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 
UIImage *img = [UIImage imageWithCGImage:cgimg]; 
CGContextRelease(ctx); 
CGImageRelease(cgimg); 
return img; 
} 

@end 

ギャラリーのキャプチャ中にこの関数を呼び出すかギャラリーから画像を選択します。

+1

こんにちは、ありがとうございます関数。しかし、潜在的な問題は、ファイルがすべてUIImageOrientationUpを返すことです。それらのどれも0以外の何も返さず、問題があります。 – user339946

+0

あなたは私の側からそれを試してくれるので、私はthjeコードを送ることができます。私はあなたのエラーを解決します。 – parag

1

イメージコードを取得していないと、iOSの座標系が反転している可能性がありますか?

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Transforms/Transforms.html

〜中略:

をあなたのビューの設定を使用ひっくり返し座標に

あなたが反転した座標を実装するために必要な最初のステップは、あなたのデフォルトの方向を決定することですビュー。反転した座標を使用する場合は、描画の前にビューの座標系を設定する2つの方法があります。

ビューのisFlippedメソッドをオーバーライドしてYESを返します。レンダリングの直前にコンテンツにフリップフロップ を適用してください。

だからあなたのビューで、あなたは-(BOOL)isFlippedを追加してみてください、それが何の違いを行う場合YESとテストを返すことができます。

1

私も同じ問題に直面していました。解決するために、この機能を使用します。

- (UIImage *)scaleAndRotateImage:(UIImage *) image { 
int kMaxResolution = 320; 

CGImageRef imgRef = image.CGImage; 

CGFloat width = CGImageGetWidth(imgRef); 
CGFloat height = CGImageGetHeight(imgRef); 


CGAffineTransform transform = CGAffineTransformIdentity; 
CGRect bounds = CGRectMake(0, 0, width, height); 
if (width > kMaxResolution || height > kMaxResolution) { 
    CGFloat ratio = width/height; 
    if (ratio > 1) { 
     bounds.size.width = kMaxResolution; 
     bounds.size.height = bounds.size.width/ratio; 
    } 
    else { 
     bounds.size.height = kMaxResolution; 
     bounds.size.width = bounds.size.height * ratio; 
    } 
} 

CGFloat scaleRatio = bounds.size.width/width; 
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); 
CGFloat boundHeight; 
UIImageOrientation orient = image.imageOrientation; 
switch(orient) { 

    case UIImageOrientationUp: //EXIF = 1 
     transform = CGAffineTransformIdentity; 
     break; 

    case UIImageOrientationUpMirrored: //EXIF = 2 
     transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); 
     transform = CGAffineTransformScale(transform, -1.0, 1.0); 
     break; 

    case UIImageOrientationDown: //EXIF = 3 
     transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); 
     transform = CGAffineTransformRotate(transform, M_PI); 
     break; 

    case UIImageOrientationDownMirrored: //EXIF = 4 
     transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); 
     transform = CGAffineTransformScale(transform, 1.0, -1.0); 
     break; 

    case UIImageOrientationLeftMirrored: //EXIF = 5 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); 
     transform = CGAffineTransformScale(transform, -1.0, 1.0); 
     transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
     break; 

    case UIImageOrientationLeft: //EXIF = 6 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); 
     transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
     break; 

    case UIImageOrientationRightMirrored: //EXIF = 7 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeScale(-1.0, 1.0); 
     transform = CGAffineTransformRotate(transform, M_PI/2.0); 
     break; 

    case UIImageOrientationRight: //EXIF = 8 
     boundHeight = bounds.size.height; 
     bounds.size.height = bounds.size.width; 
     bounds.size.width = boundHeight; 
     transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); 
     transform = CGAffineTransformRotate(transform, M_PI/2.0); 
     break; 

    default: 
     [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; 

} 

UIGraphicsBeginImageContext(bounds.size); 

CGContextRef context = UIGraphicsGetCurrentContext(); 

if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
    CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
    CGContextTranslateCTM(context, -height, 0); 
} 
else { 
    CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
    CGContextTranslateCTM(context, 0, -height); 
} 

CGContextConcatCTM(context, transform); 

CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return imageCopy; 
} 
関連する問題