iOS 相机拍摄的照片上传时被逆旋转90度

项目中出现一个问题,苹果原相机拍的照片个别会出现图片被逆旋转90度,也就是说明明是竖着的照片,上传服务器后,就变成横着的了。

经过一系列排查发现,被逆旋转的照片是在选中照片后就被旋转了,在回调里有一个UIImageOrientation图片属性

      [[PHImageManagerdefaultManager]requestImageDataForAsset:self.assetoptions:optionsresultHandler:^(NSData*_Nullable imageData,NSString*  _Nullable  dataUTI,   UIImageOrientation  orientation,   NSDictionary*_Nullable   info) 

UIImageOrientation属性

在实际开发中,如果图片是正常的,UIImageOrientation 属性返回0,也就是UIImageOrientationUp;那同理,如果图片被逆旋转90度,UIImageOrientation 属性返回3即UIImageOrientationLeft。

知道了原因就有解决的方向了,写一个UIImage的工具类,将被旋转的图片再旋转回来就好了。

//防止图片被旋转90度

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


    if (aImage.imageOrientation == UIImageOrientationUp)


        returnaImage;


    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;

    }

    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 = [UIImageimageWithCGImage:cgimg];

    CGContextRelease(ctx);

    CGImageRelease(cgimg);

    returnimg;

}

以上即可解决。

你可能感兴趣的:(iOS 相机拍摄的照片上传时被逆旋转90度)