iOS开辟- 相机(摄像头)获取到的图形自动旋转90度消除办法

前日写demo的时候发现, 假诺把经过相机得到到的图形,间接开始展览操作,
例如裁剪, 缩放, 则会把原图片向又转动90度。

刚初步以为莫明其妙, 心中无数。 后来百度了弹指间,找到了化解办法。

ps: 查找进度中, 碰到了一种说法:

//get original photo from iOS photos 
//如果该图片大于2M,会自动旋转90度;否则不旋转
UIImage* originalImg=[dict objectForKey:UIImagePickerControllerOriginalImage];

至于是或不是正确, 还没明确。 先马克。

上面包车型地铁化解办法亲测可行。
最初的作品:

用相机拍录出来的肖像含有EXIF新闻,UIImage的imageOrientation属性指的就是EXIF中的orientation音讯。
若果大家忽略orientation音信,而一直对照片举行像素管理依然drawInRect等操作,获得的结果是扭曲可能旋转90随后的标准。那是因为我们实行像素管理或然drawInRect等操作之后,imageOrientaion消息被剔除了,imageOrientaion被重设为0,产生照片内容和imageOrientaion不匹配。
故此,在对照片举行管理以前,先将照片旋转到科学的自由化,而且重返的imageOrientaion为0。
上面那几个方法便是一个UIImage category中的方法,用它能够高达以上目标。

- (UIImage *)fixOrientation:(UIImage *)aImage {

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

    // 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 (aImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

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

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

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

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

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

        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.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;
}

相机(录制头)获取到的图样自动旋转90度消除办法 前几天写demo的时候开采,
假使把通过相机得到到的图样,直接开展操作, 举例裁剪…

做项目标时候开掘, 假诺把经过相机得到到的图片,直接开始展览操作, 比如裁剪,
缩放, 恐怕会把原图片向右旋转90度。
用相机拍照出来的相片含有EXIF音信,UIImage的imageOrientation属性指的正是EXIF中的orientation新闻。
只要大家忽略orientation消息,而间接对照片举办像素管理恐怕drawInRect等操作,获得的结果是扭曲只怕旋转90从此的旗帜。那是因为我们执行像素管理可能drawInRect等操作之后,imageOrientaion消息被去除了,imageOrientaion被重设为0,形成照片内容和imageOrientaion不相配。
从而,在对照片进行拍卖在此以前,先将照片旋转到科学的样子,並且重临的imageOrientaion为0。

本文转发自这里:http://blog.csdn.net/hitwhylz/article/details/39518463

相关:
1.iOS中(相册)视频头拿走的图纸上传至服务器被自动旋转了
2.One plus上关于相机拍照的图样的imageOrientation的主题材料

上边那些点子正是二个UIImage category中的方法,用它能够达成获得科学照片方向的指标。

- (UIImage *)fixOrientation:(UIImage *)aImage {  

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

    // 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 (aImage.imageOrientation) {  
        case UIImageOrientationDown:  
        case UIImageOrientationDownMirrored:  
            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);  
            transform = CGAffineTransformRotate(transform, M_PI);  
            break;  

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

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

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

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

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

        default:  
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.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;  
}

但有时我们赢得图片时用的是AGImagePickerController,在其从ALAsset转为UIImage的时候大概原图就发出了旋转,由此,这里也交给了二个管理措施:

            for (ALAsset * asset in self.assetArray) {
                CGImageRef ref = [[asset defaultRepresentation]fullResolutionImage];
                UIImage *orgImage = [UIImage imageWithCGImage:[asset.defaultRepresentation fullScreenImage]
                                                        scale:[asset.defaultRepresentation scale] orientation:
                                     (UIImageOrientation)[asset.defaultRepresentation orientation]];
                // 这句解决了问题
                orgImage = [UIImage imageWithCGImage:ref scale:1 orientation:orgImage.imageOrientation];
//                XMLog(@"原始选完imageOrientation===%ld",orgImage.imageOrientation);
                // 让图片的旋转方向向上(即正确的方向)
                orgImage = [self fixOrientation: orgImage];
                [self.previewImageArray addObject:orgImage];
            }

            [delegate imagePickerDidSelectImagearray:self.previewImageArray];

但一旦一起始用以下这种写法从ALAsset转为UIImage,就不会发生旋转的标题:

    ALAssetRepresentation *assetRep = [asset defaultRepresentation];
                CGImageRef imgRef = [assetRep fullResolutionImage];
                UIImage *orgImage = [UIImage imageWithCGImage:imgRef
                                                   scale:assetRep.scale
                                             orientation:(UIImageOrientation)assetRep.orientation];

参照他事他说加以考察资料:资料1

资料2

前几天写demo的时候开采, 要是把通过相机获得到的图片,间接开始展览操作,
举例裁剪, 缩放, 则会把原图片向又转悠90度。
刚伊始感到无缘无故, 胸中无数。 后来百度了一晃,找到了消除办法。

原因:

Author

发表评论

电子邮件地址不会被公开。 必填项已用*标注