本文转载至 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:

  1. UIImage *originalImage = [... whatever ...];
  2. UIImage *imageToDisplay =
  3. [UIImage imageWithCGImage:[originalImage CGImage]
  4. scale:1.0
  5. 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:

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

  1. - (UIImage *)normalizedImage {
  2. if (self.imageOrientation == UIImageOrientationUp) return self;
  3. UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
  4. [self drawInRect:(CGRect){0, 0, self.size}];
  5. UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
  6. UIGraphicsEndImageContext();
  7. return normalizedImage;
  8. }

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:

  1. #define rad(angle) ((angle) / 180.0 * M_PI)
  2. - (CGAffineTransform)orientationTransformedRectOfImage:(UIImage *)img
  3. {
  4. CGAffineTransform rectTransform;
  5. switch (img.imageOrientation)
  6. {
  7. case UIImageOrientationLeft:
  8. rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -img.size.height);
  9. break;
  10. case UIImageOrientationRight:
  11. rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -img.size.width, 0);
  12. break;
  13. case UIImageOrientationDown:
  14. rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -img.size.width, -img.size.height);
  15. break;
  16. default:
  17. rectTransform = CGAffineTransformIdentity;
  18. };
  19. return CGAffineTransformScale(rectTransform, img.scale, img.scale);
  20. }
  21. - (UIImage *)croppedImage:(UIImage*)orignialImage InRect:(CGRect)visibleRect{
  22. //transform visible rect to image orientation
  23. CGAffineTransform rectTransform = [self orientationTransformedRectOfImage:orignialImage];
  24. visibleRect = CGRectApplyAffineTransform(visibleRect, rectTransform);
  25. //crop image
  26. CGImageRef imageRef = CGImageCreateWithImageInRect([orignialImage CGImage], visibleRect);
  27. UIImage *result = [UIImage imageWithCGImage:imageRef scale:orignialImage.scale orientation:orignialImage.imageOrientation];
  28. CGImageRelease(imageRef);
  29. return result;
  30. }

iOS - UIImageView - how to handle UIImage image orientation的更多相关文章

  1. [iOS]UIImageView增加圆角

    [iOS]UIImageView增加圆角 "如何给一个UIImageView增加圆角?有几种方法?各自区别?" 备注:本文参考自http://www.jianshu.com/p/d ...

  2. iOS - UIImageView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImageView : UIView @available(iOS 2.0, *) public class U ...

  3. iOS UIImageView设置为圆形

    UIImageView设置为圆形的方法(效率比较低下,当需要显示很多圆形view的时候,非常不推荐这种方式): imageView.layer.masksToBounds = YES; imageVi ...

  4. iOS:由URL成员UIImage

    很多时候,我们只能得到URL.然后,需要建立一个UIImage. 在正常情况下,.我们一般通过SDWebImage直接施工UIImageVIew的image,如何使用URL直接施工UIImage它? ...

  5. iOS UIImageView自适应图片大小

    窗口大小获取: CGRect screenBounds = [ [UIScreenmainScreen]bounds];//返回的是带有状态栏的Rect CGRect rect = [ [UIScre ...

  6. ios UIImageView异步加载网络图片

    方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...

  7. ios UIImageView处理图片大小问题

    UIImageView视图可以显示图片 实例化UIImageView有两种方法 第一种方法: UIImageView *myImageView = [[ UIImageView alloc] init ...

  8. iOS - UIImageView 动画

    1.UIImageView 动画 1.1 播放图片集 播放图片集 @property (nonatomic, strong) UIImageView *playImageView; self.play ...

  9. iOS:UIImageView图像视图控件

    UIImageView:图像视图控件:    它是UIView的子类,因此也是视图控件,可以用来显示图像.因为它具有帧动画属性和操作方法,因此可以用来制作动画,其实动画就是很短的时间内,执行显示连续的 ...

随机推荐

  1. 一款纯css3实现的超炫动画背画特效

    之前为大家介绍了很多款由纯css3实现的特效.今天要再给大家带来一款纯css3实现的超炫动画背画特效.代码非常简单,没有引用任何其它js代码.css代码也不多.效果非常炫.一起看下效果图: 在线预览  ...

  2. 【C#/WPF】窗体定时自动关闭

    需求:打开WPF项目后,展示3秒钟产品Logo后,进入主界面MainWindow.(类似于安卓应用打开时的闪屏页SplashPage) 思路:在进入MainWindow后新建一个Window窗体,窗体 ...

  3. git学习(四):理解git暂存区(stage)

    与一般的版本管理不同的是,git在提交之前要将更改通过git add 添加到暂存区才能提交(git commit).即使是已经交给了git来管理的文件也是如此.这里继续学习git的暂存区. 通过git ...

  4. 单网卡绑定多个ip, 多个网卡绑定成一块虚拟网卡

    Linux网卡配置与绑定   Redhat Linux的网络配置,基本上是通过修改几个配置文件来实现的,虽然也可以用ifconfig来设置IP,用route来配置默认网关,用hostname来配置主机 ...

  5. iOS边练边学--(Quartz2D)图片裁剪,带圆环的裁剪

    一.图片裁剪,示意图 二.带圆环的图片裁剪示意图

  6. tp-03 模板显示

    方式模板:$this->display(); 每当建立一个控制器  都要在view建立一个名字相对应的文件夹   在建立相对于的页面.

  7. easyui中datagrid用法,加载table数据与标题

    加载标题写法: 多行标题:columns: [[ columns: [[                       { field: 'itemid', title: 'Item ID', rows ...

  8. 关于Cocos2d-x中节点的获取

    方法一: 1.在.h文件的属性里面先声明要使用的节点或者变量. private: Label *scorelabel; 2.在.cpp文件中创建并使用这个节点或者变量. scorelabel = La ...

  9. Android ArryaList 笔记

    Arraylist相当于动态数组,可以动态的添加或者删除其中的元素. 参考链接 http://beginnersbook.com/2013/12/java-arraylist/ package com ...

  10. The configuration file 'appsettings.json' was not found and is not optional

    问: .Net Core: Application startup exception: System.IO.FileNotFoundException: The configuration file ...