设为首页 加入收藏

TOP

iOS开发--解决拍照后照片旋转90度的问题(附Swift版本)(一)
2017-10-11 18:35:52 】 浏览:4717
Tags:iOS 开发 解决 拍照 照片 旋转 问题 Swift 版本

当我们使用相机拍照后,调用UIImagePickerController的delegate方法,如下代码所示:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
}

通过UIImagePickerControllerOriginalImage这个key从info字典中可以获取到拍照后的UIImage,但如果断点查看图片,我们会发现图片是旋转了90度的。

不过如果直接使用图片,比如使用这个UIImage赋值给一个UIImageView的实例,显示出来的图片是完全没有问题的。

但如果使用这个UIImage作他用,比如上传到服务器,就会出现问题了,上传到服务器的图片也是旋转了90度的。

 

原因:通过UIImagePickerControllerOriginalImage这个key从info字典中可以获取到拍照后的UIImage,其imageOrientation属性默认是UIImageOrientationRight,也就是说默认就是向左侧旋转了90度的。


解决方法如下:

方法一:创建UIImagePickerController实例的时候,设置实例的一个属性allowsEditing为YES,设了这个值,拍完照片后会在照片上出现一个框框,可以对照片的裁剪编辑。在UIImagePickerController的代理方法中取照片的时候也就别用UIImagePickerControllerOriginalImage这个key来取了,而要用UIImagePickerControllerEditedImage,此时取出来的照片,它的imageOrientation是UIImageOrientationUp而不是默认的UIImageOrientationRight

 

方法二:有一个专门针对UIImage旋转这个问题的很好的Category,代码如下:

OC版本:

UIImage+fixOrientation.h

#import <UIKit/UIKit.h>

@interface UIImage (fixOrientation)

- (UIImage *)fixOrientation;

@end

UIImage+fixOrientation.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 UIImageOrienta
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS 字符串 MD5 下一篇【iOS】7.4 定位服务->3.4 地..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目