zoukankan      html  css  js  c++  java
  • IOS8 UIImagePickerController 拍照自动旋转的问题

      在IOS开发中,当我们需要拍照时,自然是弹出一个UIImagePickerController,并且设置sourceType=UIImagePickerControllerSourceTypeCamera。

    然后再代理方法中用 UIImage *img=info[UIImagePickerControllerOriginalImage]来获取拍摄的图像,然后显示在界面或者转换成JPG上传到服务器

    NSData *imgdata=UIImageJPEGRepresentation(fiximg, 0.75)(如果是PNG可能没问题)。

      以前一直这么用,没有发现问题,可最近安装了XCODE6使用IOS8SDK的时候,发现上传通过以上方法上传的图片居然自动旋转了!!在IOS设备和MAC电脑

    上显示是正常的,可是到了安卓设备或者windows确是逆时针旋转90度的照片。通过网上搜索,找到下面这个方法解决这个问题。新建一个UIImage Category.

    .h文件

    @interface UIImage (fixOrientation)
    - (UIImage *)fixOrientation;
    @end
    

     .m文件

    #import "UIImage+fixOrientation.h"
    
    @implementation UIImage (fixOrientation)
    
    - (UIImage *)fixOrientation {
        
        // No-op if the orientation is already correct
        if (self.imageOrientation == UIImageOrientationUp) return self;
        
        // 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 (self.imageOrientation) {
            case UIImageOrientationDown:
            case UIImageOrientationDownMirrored:
                transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
                transform = CGAffineTransformRotate(transform, M_PI);
                break;
                
            case UIImageOrientationLeft:
            case UIImageOrientationLeftMirrored:
                transform = CGAffineTransformTranslate(transform, self.size.width, 0);
                transform = CGAffineTransformRotate(transform, M_PI_2);
                break;
                
            case UIImageOrientationRight:
            case UIImageOrientationRightMirrored:
                transform = CGAffineTransformTranslate(transform, 0, self.size.height);
                transform = CGAffineTransformRotate(transform, -M_PI_2);
                break;
        }
        
        switch (self.imageOrientation) {
            case UIImageOrientationUpMirrored:
            case UIImageOrientationDownMirrored:
                transform = CGAffineTransformTranslate(transform, self.size.width, 0);
                transform = CGAffineTransformScale(transform, -1, 1);
                break;
                
            case UIImageOrientationLeftMirrored:
            case UIImageOrientationRightMirrored:
                transform = CGAffineTransformTranslate(transform, self.size.height, 0);
                transform = CGAffineTransformScale(transform, -1, 1);
                break;
        }
        
        // Now we draw the underlying CGImage into a new context, applying the transform
        // calculated above.
        CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
                                                 CGImageGetBitsPerComponent(self.CGImage), 0,
                                                 CGImageGetColorSpace(self.CGImage),
                                                 CGImageGetBitmapInfo(self.CGImage));
        CGContextConcatCTM(ctx, transform);
        switch (self.imageOrientation) {
            case UIImageOrientationLeft:
            case UIImageOrientationLeftMirrored:
            case UIImageOrientationRight:
            case UIImageOrientationRightMirrored:
                // Grr...
                CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
                break;
                
            default:
                CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.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;
    }
    
    @end
    

     调用方式:

          UIImage *img=info[UIImagePickerControllerOriginalImage];

          UIImage *fiximg=[img fixOrientation];

  • 相关阅读:
    pat 09-排序1. 排序(25)
    pat 06-图2. Saving James Bond
    hdu 2818 Building Block
    并查集
    hdu 1213 How Many Tables
    hdu 1232 畅通工程
    hdu 1258 Sum It Up
    如何查看Navicat 查询结果
    Navicat查询功能怎么用
    如何设置Navicat 筛选向导
  • 原文地址:https://www.cnblogs.com/Rmeo/p/4234093.html
Copyright © 2011-2022 走看看