iOS开发- 相机拍照获取到的图片自动旋转90度的解决办法

2018-08-10  本文已影响0人  iOS开发美学

问题

今天在做项目的时候发现, 如果把通过相机获取到的图片,直接进行操作, 比如裁剪, 缩放, 则会把原图片向又旋转90度。


产生原因

1、如果该图片大于2M,会自动旋转90度;否则不旋转

UIImage* originalImg=[dict objectForKey:UIImagePickerControllerOriginalImage];

2、用相机拍摄出来的照片含有EXIF信息,UIImage的imageOrientation属性指的就是EXIF中的orientation信息。如果我们忽略orientation信息,而直接对照片进行像素处理或者drawInRect等操作,得到的结果是翻转或者旋转90之后的样子。这是因为我们执行像素处理或者drawInRect等操作之后,imageOrientaion信息被删除了,imageOrientaion被重设为0,造成照片内容和imageOrientaion不匹配。


解决办法

UIImage Category中写一个处理图片方向的方法,然后在相机拍照获取到图片后调用,就可以解决原始图片旋转90度的问题了,下面是UIImage类别的方法源码,可直接复制调用哦。

+ (UIImage*)fixOrientation:(UIImage*)image

{

    if (image.imageOrientation == UIImageOrientationUp)

        return image;

    CGAffineTransform transform = CGAffineTransformIdentity;

    switch (image.imageOrientation) {

        case UIImageOrientationDown:

        case UIImageOrientationDownMirrored:

            transform =CGAffineTransformTranslate(transform, image.size.width, image.size.height);

            transform =CGAffineTransformRotate(transform,M_PI);

            break;

        case UIImageOrientationLeft:

        case UIImageOrientationLeftMirrored:

            transform =CGAffineTransformTranslate(transform, image.size.width,0);

            transform =CGAffineTransformRotate(transform,M_PI_2);

            break;

        case UIImageOrientationRight:

        case UIImageOrientationRightMirrored:

            transform =CGAffineTransformTranslate(transform,0, image.size.height);

            transform =CGAffineTransformRotate(transform, -M_PI_2);

            break;

        default:

            break;

    }

    switch (image.imageOrientation) {

        case UIImageOrientationUpMirrored:

        case UIImageOrientationDownMirrored:

            transform =CGAffineTransformTranslate(transform, image.size.width,0);

            transform =CGAffineTransformScale(transform, -1,1);

            break;

        case UIImageOrientationLeftMirrored:

        case UIImageOrientationRightMirrored:

            transform =CGAffineTransformTranslate(transform, image.size.height,0);

            transform =CGAffineTransformScale(transform, -1,1);

            break;

        default:

            break;

    }

    CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,

                                             CGImageGetBitsPerComponent(image.CGImage),0,

                                             CGImageGetColorSpace(image.CGImage),

                                             CGImageGetBitmapInfo(image.CGImage));

    CGContextConcatCTM(ctx, transform);

    switch (image.imageOrientation) {

        case UIImageOrientationLeft:

        case UIImageOrientationLeftMirrored:

        case UIImageOrientationRight:

        case UIImageOrientationRightMirrored:

            // Grr...

            CGContextDrawImage(ctx,CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);

            break;

        default:

            CGContextDrawImage(ctx,CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);

            break;

    }

    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);

    UIImage*img = [UIImage imageWithCGImage:cgimg];

    CGContextRelease(ctx);

    CGImageRelease(cgimg);

    return img;

}

上一篇下一篇

猜你喜欢

热点阅读