别忽视UIImage中的方向属性, imageOrientation

转载 2012年09月14日 11:38:15

这几天写个拍照,或者从相册中选择照片,进行剪切,然后分享.结果出现了,剪切后图片颠倒或者旋转90度的问题.

找了很久才发现是忽略imageOrientation这个属性.

以下为解决方法:

+ (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;
}

参考:http://www.cnblogs.com/jiangyazhou/archive/2012/03/22/2412343.html


如果用用UIGraphicsBeginImageContext 去重新绘制也可以解决,但是处理速度实在太慢.

python全栈开发devops方向初到高级

-
  • 1970年01月01日 08:00

UIImage旋转详解

本文讲解UIImage的旋转方法,是UIImage,不是UIImageView!!! 主要用途:手机横屏拍照,需要得到一张width大于height的照片。或者其他用途 说实在话,图片旋转的逻...
  • gang544043963
  • gang544043963
  • 2015-11-12 17:06:35
  • 3400

UIImage的imageOrientation属性 修改图片旋转了的bug

本回要从我们项目说起,今天测试的给我叫去,问我这个图片裁剪是不是有BUG,从手机相册里选的图(iOS设备)裁剪出来怎么就.....(省略好多字)。 当时本大侠就想 NND当时我做的时候就用模...
  • u013430014
  • u013430014
  • 2015-05-28 15:23:24
  • 3285

iOS开发中关于UIImage的知识点总结

本文是转载文章
  • Ordinary_Rookie
  • Ordinary_Rookie
  • 2016-04-07 14:28:09
  • 425

UIImage改变图片方向

+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientati...
  • qq_25737881
  • qq_25737881
  • 2016-09-20 00:50:07
  • 744

iOS开发 UIimage旋转,得到旋转后的Image图片,解决imageView旋转,内部图片不跟着旋转问题

一:前言  1.0  : 图片处理中,比如对一个ImageView进行旋转,那么旋转后图片缺还是原来的样子,本文就是解决图片旋转的问题  1.0 :无demo说话不硬气,上github地址:htt...
  • horisea
  • horisea
  • 2017-04-21 15:21:53
  • 2986

UIImage常用属性和方法

一、属性 1、size: reflects orientation setting. In iOS 4.0 and later, this is measured in points. In 3.x...
  • BlackWolfSky
  • BlackWolfSky
  • 2016-05-03 10:41:19
  • 1356

ios 屏幕旋转Orientation总结2

参考总结1的内容发现在Ios78中,覆盖supportedInterfaceOrientations方法无效。正确的做法是覆盖以下方法: 自定义一个UINavigationController的子类,...
  • dlmlzz09
  • dlmlzz09
  • 2015-03-11 09:22:46
  • 1664

iOS 相册图片,相机图片被旋转问题 uiimage orientation

把 - (UIImage *)fixOrientation:(UIImage *)aImage { // No-op if the orientation is already...
  • u010698385
  • u010698385
  • 2014-08-05 15:28:53
  • 698

如何处理iOS中照片的方向

http://feihu.me/blog/2015/how-to-handle-image-orientation-on-iOS/
  • winsdom123456
  • winsdom123456
  • 2015-06-05 09:43:47
  • 1923
收藏助手
不良信息举报
您举报文章:别忽视UIImage中的方向属性, imageOrientation
举报原因:
原因补充:

(最多只允许输入30个字)