为啥此裁剪代码会导致图像旋转?

Posted

技术标签:

【中文标题】为啥此裁剪代码会导致图像旋转?【英文标题】:Why Does this Cropping Code Result in a Rotated Image?为什么此裁剪代码会导致图像旋转? 【发布时间】:2012-10-08 14:48:28 【问题描述】:

这个问题的标题应该很清楚。下面的代码执行裁剪操作,然后在图像视图中显示新裁剪的照片。问题是图像一旦被裁剪,就会以与原始源图像不同的方向显示。这是为什么?我该怎么办?

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info

[self.popoverController dismissPopoverAnimated:true];

NSString *mediaType = [info
                       objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
    UIImage *image = [info
                      objectForKey:UIImagePickerControllerOriginalImage];


    CGRect rect = CGRectMake(0, ((image.size.height - image.size.width) / 2), image.size.width, image.size.width);


    CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
    CGRect smallBounds = CGRectMake(rect.origin.x, rect.origin.y, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));

    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    croppedImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();

    imageView.image = croppedImage;

*编辑

根据 cmets 表明问题的根本原因是删除了 EXIF 标记,我尝试使用以下代码更正方向。这仍然不能解决问题,但我认为这是朝着正确方向迈出的一步。也许它会帮助某人为这个问题提出一个新的答案。

if (image.imageOrientation == UIImageOrientationLeft) 
        NSLog(@"left");
        CGContextRotateCTM (context, radians(90));

     else if (image.imageOrientation == UIImageOrientationRight) 
        NSLog(@"right");
        CGContextRotateCTM (context, radians(-90));

     else if (image.imageOrientation == UIImageOrientationUp) 
        NSLog(@"up");
        // NOTHING
     else if (image.imageOrientation == UIImageOrientationDown) 
        NSLog(@"down");
        CGContextRotateCTM (context, radians(-180.));
    

见https://***.com/a/5184134/549273

【问题讨论】:

我相信Apple使用EXIF标签来实现照片的旋转。您的新图像可能缺少该方向信息,因此显示不正确。 图片来自 iPhone 摄像头,所以它应该有一个 EXIF 标签。 在将 exif 数据绘制到上下文中时,您没有将其考虑在内。图像将始终处于其“原始”方向。 UIImage 根据 exif 旋转它。除了您将其原始绘制到没有 exif 数据的新 UIImage 中,因此“未旋转”。 有没有办法通过上下文保存EXIF数据? 【参考方案1】:

我认为您最好的选择是从传入的图像中获取方向,然后使用适当的旋转创建新图像。

从传入的信息字典中获取元数据:

NSDictionary *metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
NSNumber *orientation = [metadata objectForKey:@"Orientation"];

然后当你创建你的UIImage:

croppedImage = [UIImage imageWithCGImage:subImageRef 
                                   scale:1.0 
                             orientation:[orientation intValue]];

希望这是你的起点,至少。

【讨论】:

【参考方案2】:

你应该做的是在处理之前修复图像方向,我遇到了同样的问题,我正在使用 swift 所以我不得不改变这个答案:

ios UIImagePickerController result image orientation after upload

但代码运行良好。请记住,它返回一个图像并且它没有编辑图像,我只是因为那个简单的错误而努力让它正确。顺便说一句,你不应该使用 UIImageOrientation 而是 UIImageOrientation.rawValue,至少在 swift 中是这样。

我将在此处留下 swift 的代码,希望它有所帮助

extension UIImage 
public func fixOrientation() -> UIImage?

    if self.imageOrientation.rawValue == UIImageOrientation.Up.rawValue 
        return self
    

    var transform : CGAffineTransform = CGAffineTransformIdentity

    switch self.imageOrientation.rawValue 

    case UIImageOrientation.Down.rawValue:
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height)
            transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
    case UIImageOrientation.DownMirrored.rawValue:
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height)
            transform = CGAffineTransformRotate(transform, CGFloat(M_PI))

    case UIImageOrientation.Left.rawValue:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0)
            transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
    case UIImageOrientation.LeftMirrored.rawValue:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0)
            transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))

    case UIImageOrientation.Right.rawValue:
            print("working on right orientation")
            transform = CGAffineTransformTranslate(transform, 0, self.size.height)
            transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))
    case UIImageOrientation.RightMirrored.rawValue:
            transform = CGAffineTransformTranslate(transform, 0, self.size.height)
            transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))

    case UIImageOrientation.Up.rawValue: break
    case UIImageOrientation.UpMirrored.rawValue: break

    default: break
    

    switch self.imageOrientation.rawValue 

    case UIImageOrientation.UpMirrored.rawValue:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0)
            transform = CGAffineTransformScale(transform, -1, 1)
    case UIImageOrientation.DownMirrored.rawValue:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0)
            transform = CGAffineTransformScale(transform, -1, 1)

    case UIImageOrientation.LeftMirrored.rawValue:
            transform = CGAffineTransformTranslate(transform, self.size.height, 0)
            transform = CGAffineTransformScale(transform, -1, 1)
    case UIImageOrientation.RightMirrored.rawValue:
            transform = CGAffineTransformTranslate(transform, self.size.height, 0)
            transform = CGAffineTransformScale(transform, -1, 1)

    case UIImageOrientation.Up.rawValue: break
    case UIImageOrientation.Down.rawValue: break
    case UIImageOrientation.Left.rawValue: break
    case UIImageOrientation.Right.rawValue: break

    default: break
    

    let bitmapInfo = CGImageGetBitmapInfo(self.CGImage)

    let ctx = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height), CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage),
        bitmapInfo.rawValue)

    CGContextConcatCTM(ctx, transform)

    switch self.imageOrientation 

    case UIImageOrientation.Left:
            CGContextDrawImage(ctx, CGRectMake(0, 0, self.size.height, self.size.width), self.CGImage)
    case UIImageOrientation.LeftMirrored:
            CGContextDrawImage(ctx, CGRectMake(0, 0, self.size.height, self.size.width), self.CGImage)
    case UIImageOrientation.Right:
            CGContextDrawImage(ctx, CGRectMake(0, 0, self.size.height, self.size.width), self.CGImage)
    case UIImageOrientation.RightMirrored:
            CGContextDrawImage(ctx, CGRectMake(0, 0, self.size.height, self.size.width), self.CGImage)

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

    

    let cgimg = CGBitmapContextCreateImage(ctx)
    let image = UIImage(CGImage: cgimg!)

    return image

    

【讨论】:

以上是关于为啥此裁剪代码会导致图像旋转?的主要内容,如果未能解决你的问题,请参考以下文章

将 UIImage 转换为 CGImage 会导致图像旋转

如何在裁剪意图期间旋转图像

旋转 90 度更改图像大小和裁剪 - Swift 3

使用 CoreImage 过滤图像会导致图像旋转

裁剪图像会导致越界错误[关闭]

在opencv中裁剪旋转图像