本文转载至 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. 14门Linux课程,打通你Linux的任督二脉!
  2. Mongodb学习笔记一(Mongodb环境配置)
  3. Matlab(2) -- Find()函数
  4. inheritableStatics 与statics类
  5. Maven 3.3.3 Win10环境下的使用实例(中)
  6. IOS 单例模式的写法
  7. Fiddler高级技巧 - 映射路径到本地文件夹
  8. DB2 重新设定表自增字段的当前值
  9. 配置Tomcat以指定的身份(非root)运行
  10. ACM一道关于素数查找的题
  11. jQuery ajax - getScript() 方法和getJSON方法
  12. java实现的简单词法分析器
  13. [html]------行内元素与块级元素
  14. redis cluster简介和配置(3)
  15. 如何解决一个从SkylineGlobe5版本升级到7版本遇到的小问题
  16. hiveserver2启动成功但无法通过beeline连接
  17. particular.js
  18. IDM的Google商店插件
  19. MT7601 WG209模块驱动移植,并连接路由器
  20. requests 请求几个接口 出现’您的账户在其它设备使用过,为保障安全,需重新登入才能在本设备使用‘

热门文章

  1. 拦截iOS系统导航栏返回按钮事件-三种方法
  2. Pro mvvm读书笔记mvvm中的VM
  3. 阿里云ECS,WampServer无法访问外网
  4. [转]ListView学习笔记(二)——ViewHolder
  5. iOS边练边学--九宫格布局
  6. 不可错过的10个超棒jQuery表单操作代码片段
  7. TypeError: datetime.datetime(2016, 9, 25, 21, 12, 19, 135649) is not JSON serializable解决办法(json无法序列化对象的解决办法)
  8. 【转】C#获取电脑客户端IP地址及当前用户名
  9. Spring Boot 官方文档学习(二)特点
  10. e664. 在图像中获取子图像