码迷,mamicode.com
首页 > 其他好文 > 详细

处理图片上下颠倒

时间:2015-08-10 17:39:41      阅读:472      评论:0      收藏:0      [点我收藏+]

标签:

  在iOS不同的Framework中,使用的是不同的坐标系:

  UIKit : Y轴向下/Core Graphics(Quartz) : y轴向上/OpenGL ES : y轴向上

  UIKit是iPhone SDK的Cocoa Touch层的核心framework,是iPhone应用程序图形界面和事件驱动的基础,它和传统的windows桌面一样,坐标系是y轴向下的;

Core Graphics(Quartz)一个基于2D的图形绘制引擎,它的坐标系则是y轴向上的;而OpenGL ES是iPhone SDK的2D和3D绘制引擎,它使用左手坐标系,它的坐

标系也是y轴向上的,如果不考虑z轴,在二维下它的坐标系和Quartz是一样的。

  另外从相机导入的图片,需要先进行方向处理!用相机拍摄出来的照片含有EXIF信息,UIImage的imageOrientation属性指的就是EXIF中的orientation信息。如

果我们忽略orientation信息,而直接对照片进行像素处理或者drawInRect等操作,得到的结果是翻转或者旋转90之后的样子。这是因为我们执行像素处理或者

drawInRect等操作之后,imageOrientaion信息被删除了,imageOrientaion被重设为0,造成照片内容和imageOrientaion不匹配。所以,在对照片进行处理之前,

先将照片旋转到正确的方向,并且返回的imageOrientaion为0。

  列举3中方法:

  1.判断图片方向,进行图片旋转

 1 - (UIImage *)fixOrientation:(UIImage *)aImage {
 2     
 3     // No-op if the orientation is already correct
 4     if (aImage.imageOrientation == UIImageOrientationUp)
 5         return aImage;
 6     
 7     CGAffineTransform transform = CGAffineTransformIdentity;
 8     
 9     switch (aImage.imageOrientation) {
10         case UIImageOrientationDown:
11         case UIImageOrientationDownMirrored:
12             transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
13             transform = CGAffineTransformRotate(transform, M_PI);
14             break;
15             
16         case UIImageOrientationLeft:
17         case UIImageOrientationLeftMirrored:
18             transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
19             transform = CGAffineTransformRotate(transform, M_PI_2);
20             break;
21             
22         case UIImageOrientationRight:
23         case UIImageOrientationRightMirrored:
24             transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
25             transform = CGAffineTransformRotate(transform, -M_PI_2);
26             break;
27         default:
28             break;
29     }
30     
31     switch (aImage.imageOrientation) {
32         case UIImageOrientationUpMirrored:
33         case UIImageOrientationDownMirrored:
34             transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
35             transform = CGAffineTransformScale(transform, -1, 1);
36             break;
37             
38         case UIImageOrientationLeftMirrored:
39         case UIImageOrientationRightMirrored:
40             transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
41             transform = CGAffineTransformScale(transform, -1, 1);
42             break;
43         default:
44             break;
45     }
46     
47     // Now we draw the underlying CGImage into a new context, applying the transform
48     // calculated above.
49     CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
50                                              CGImageGetBitsPerComponent(aImage.CGImage), 0,
51                                              CGImageGetColorSpace(aImage.CGImage),
52                                              CGImageGetBitmapInfo(aImage.CGImage));
53     CGContextConcatCTM(ctx, transform);
54     switch (aImage.imageOrientation) {
55         case UIImageOrientationLeft:
56         case UIImageOrientationLeftMirrored:
57         case UIImageOrientationRight:
58         case UIImageOrientationRightMirrored:
59             // Grr...
60             CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
61             break;
62             
63         default:
64             CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
65             break;
66     }
67     
68     // And now we just create a new UIImage from the drawing context
69     CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
70     UIImage *img = [UIImage imageWithCGImage:cgimg];
71     CGContextRelease(ctx);
72     CGImageRelease(cgimg);
73     return img;
74 }

 

  2.使用UIImage的drawInRect函数,该函数内部能自动处理图片的正确方向

- (UIImage *)normalizedImage:(UIImage *)noImage {
   if (noImage.imageOrientation == UIImageOrientationUp) return noImage;

     UIGraphicsBeginImageContextWithOptions(noImage.size, NO, noImage.scale);
    [noImage drawInRect:(CGRect){0, 0, noImage.size}];
     UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return normalizedImage;
}

  3.使用CGcontext绘制图片

CGContextTranslateCTM(context, 0, height);  
CGContextScaleCTM(context, 1.0, -1.0);  
CGContextDrawImage(context, CGRectMake(0, 0, width, height), uiImage.CGImage);  

 

处理图片上下颠倒

标签:

原文地址:http://www.cnblogs.com/jinyaowei/p/4718309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!