本文转载至 http://stackoverflow.com/questions/8915630/ios-uiimageview-how-to-handle-uiimage-image-orientation
 
 
 
 

Is it possible to setup UIImageView to handle image orientation? When I set the UIImageView to image with orientation RIGHT (it is photo from camera roll), the image is rotated to right, but I want to show it in proper orientation, as it was taken.

I know I can rotate image data but it is possible to do it more elegant?

Thank you

asked Jan 18 '12 at 18:49
Martin Pilch
1,14021441
 

4 Answers

If I understand, what you want to do is disregard the orientation of the UIImage? If so then you could do this:

UIImage *originalImage = [... whatever ...];

UIImage *imageToDisplay =
[UIImage imageWithCGImage:[originalImage CGImage]
scale:1.0
orientation: UIImageOrientationUp];

So you're creating a new UIImage with the same pixel data as the original (referenced via its CGImage property) but you're specifying an orientation that doesn't rotate the data.

answered Jan 18 '12 at 19:05
Tommy
69.7k9107125
 
1  
By the way, how can I actually rotate the image data? –  Eno Mar 8 '12 at 7:19
3  
I guess you'd create a suitably sized CGContext with CGBitmapContextCreate (or with theUIGraphicsBeginImageContext shorthand), use CGContextRotateCTM to set a rotation, use eitherdrawInRect: on the UIImage or CGContextDrawImage with the image's CGImage property, then convert the context to an image either with UIGraphicsGetImageFromCurrentImageContext (and, then,UIGraphicsEndImageContext) if you used UIKit to create the context, orCGBitmapContextCreateImage if you were sticking with the Core Graphics. UIKit isn't very thread safe, but the code would be neater. –  Tommy Mar 8 '12 at 20:31
    
Thanks, I've got it over! –  Eno Mar 10 '12 at 8:10

As Anomie suggests in their answer here, this code will help to fix orientation:

- (UIImage *)fixrotation:(UIImage *)image{

    if (image.imageOrientation == UIImageOrientationUp) return image;
CGAffineTransform transform = CGAffineTransformIdentity; switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break; case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break; case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, image.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
} switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break; case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, image.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
break;
} // Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
CGImageGetBitsPerComponent(image.CGImage), 0,
CGImageGetColorSpace(image.CGImage),
CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(ctx, transform);
switch (image.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
break; default:
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.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; }
answered Feb 23 '13 at 10:44
 
14  
1  
It's kinda sad how he got 11 up votes for plagiarism. –  Rambatino Nov 17 '14 at 19:56
1  
Also note that CGContextDrawImage takes about 40MB for a 5MB image... –  Yerk Mar 9 at 19:12 
    
@Yerk do you know what I can do instead, to stop this huge file increase? –  TimWhiting Apr 1 at 10:56

You can completely avoid manually doing the transforms and scaling yourself, as suggested by an0 in this answer here:

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

The documentation for the UIImage methods size and drawInRect explicitly states that they take into account orientation.

answered Jul 15 '14 at 23:57
user2067021
1,028916
 

here is a workable sample cod, considering the image orientation:

#define rad(angle) ((angle) / 180.0 * M_PI)
- (CGAffineTransform)orientationTransformedRectOfImage:(UIImage *)img
{
CGAffineTransform rectTransform;
switch (img.imageOrientation)
{
case UIImageOrientationLeft:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -img.size.height);
break;
case UIImageOrientationRight:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -img.size.width, 0);
break;
case UIImageOrientationDown:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -img.size.width, -img.size.height);
break;
default:
rectTransform = CGAffineTransformIdentity;
}; return CGAffineTransformScale(rectTransform, img.scale, img.scale);
} - (UIImage *)croppedImage:(UIImage*)orignialImage InRect:(CGRect)visibleRect{
//transform visible rect to image orientation
CGAffineTransform rectTransform = [self orientationTransformedRectOfImage:orignialImage];
visibleRect = CGRectApplyAffineTransform(visibleRect, rectTransform); //crop image
CGImageRef imageRef = CGImageCreateWithImageInRect([orignialImage CGImage], visibleRect);
UIImage *result = [UIImage imageWithCGImage:imageRef scale:orignialImage.scale orientation:orignialImage.imageOrientation];
CGImageRelease(imageRef);
return result;
}

最新文章

  1. C/C++ makefile自动生成工具(comake2,autotools,linux),希望能为开源做点微薄的贡献!
  2. 4、python列表
  3. mysql 基本
  4. Vim魔法堂:认识快捷键绑定
  5. sqlzoo.net刷题5
  6. svn分支管理进行迭代开发
  7. scanf
  8. C/C++程序猿必须熟练应用的开源项目
  9. AIDL实例
  10. INSERT DELAYED 句法
  11. React 入门最好的实例-TodoList
  12. 自动同步Android源代码的脚本(repo sync)
  13. AsyncSocket长连接棒包装问题解决
  14. android 组件内部实现触摸事件,更改背景
  15. 团队作业8——第二次项目冲刺(Beta阶段)Day5--5.23
  16. Python基础之Windows下Python3.x环境搭建
  17. markdown实战问题备忘
  18. [升级说明] Senparc.Weixin.MP v14.8.11 (微信群发接口调整)
  19. 洗礼灵魂,修炼python(69)--爬虫篇—番外篇之feedparser模块
  20. Java虚拟机(Java Virtual Machine)

热门文章

  1. Android View的绘制流程
  2. Linux 用户添加sudo 权限
  3. 移植SlidingMenu Android library,和安装example出现的问题解决
  4. 包介绍 - Fluent Validation (用于验证)
  5. 源自梦想 自定义ViewGroup的整理_1
  6. asp.net的CascadingDropDown取值和赋值
  7. android学习日记24--Android 菜单开发
  8. 【UVA10829】 L-Gap Substrings (后缀数组)
  9. 变量-if else while-运算符
  10. php 自己写的好看的分页类
  11. ZooKeeper安装与运行
  12. winform正在使用dsoframer迅速"Unable to display the inactive document.Click here to reacitive the document."
  13. C 语言字符数组的定义与初始化
  14. vue.js开发环境搭建以及创建一个vue实例
  15. Android使用HttpUrlConnection请求服务器发送数据详解
  16. [十一]基础数据类型之Character
  17. 自定义函数hello,并注册到hive源码中并重新编译
  18. 解决importerror no module named mysqldb
  19. Jmeter接口测试+压力测试+环境配置+证书导出
  20. List,Set,Collection,Collections比较