美文网首页
iOS竖屏拍照图片翻转90度解决办法

iOS竖屏拍照图片翻转90度解决办法

作者: Bobo_Ma | 来源:发表于2018-10-18 18:13 被阅读0次

    问题描述:

        拍照完成后如果直接使用图片比如使用这个UIImage赋值给一个UIImageView的实例,显示出来的图片是完全没有问题的。但如果使用这个UIImage作他用,比如上传到服务器,就会出现问题了,上传到服务器的图片也是旋转了90度的。
    

    问题原因:

        通过UIImagePickerControllerOriginalImage这个key从info字典中可以获取到拍照后的UIImage,它本身的imageOrientation属性是3,即是UIImageOrientationRight。如果对它进行裁剪、缩放等操作后,它的这个imageOrientation属性会变成0,造成照片内容和imageOrientaion不匹配。此时这张图片用在别的地方就会发生旋转。imageOrientation是只读的,不能直接修改其值。
    

    解决办法

    1.  设置相机的一个属性allowsEditing为YES,设了这个值,你拍完照片后会在照片上出现一个框框,这就是对照片的裁剪编辑。在相机的代理方法中取照片的时候就别用UIImagePickerControllerOriginalImage来取了,要用UIImagePickerControllerEditedImage。用这个key取出来的照片,它的imageOrientation是0,所以之后的任何裁剪、缩放操作都不会造成旋转。这是第一种方法。
    
    2.  在对照片进行处理之前,先将照片旋转到正确的方向,并且返回的imageOrientaion为0, 方法如下:
    
    -(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;
    }
    
    
    

    相关文章

      网友评论

          本文标题:iOS竖屏拍照图片翻转90度解决办法

          本文链接:https://www.haomeiwen.com/subject/cdyszftx.html