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

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. 模板方法模式(Head first 设计模式——7)

    一.模板方法模式定义 模板方法模式:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤. 从定义中,应该可以看出一部分,为 ...

  2. 使用 LocalReport 对象进行打印

    添加引用   从“项目”菜单中,选择“添加引用”.将显示“添加引用”对话框. 从“.NET”选项卡上显示的列表框中,选择 Winforms 和 Drawing 组件. 添加代码 应打开 Program ...

  3. C语言 · 友好数

    算法训练 友好数   时间限制:1.0s   内存限制:256.0MB      问题描述 有两个整数,如果每个整数的约数和(除了它本身以外)等于对方,我们就称这对数是友好的.例如: 9的约数和有:1 ...

  4. MySQL做为手动开启事务用法

    START TRANSACTION;INSERT INTO `t1` (t, t1) VALUES('124', NOW());ROLLBACK;COMMIT;

  5. linux 批量创建用户获取8位随机密码

    #创建账号  分组不分组只有一列useradd无法添加三个账号 添加账号  获取密码  执行最后一句echo stu{4..6}|xargs -n 1|sed -r 's#(.*)#useradd \ ...

  6. golang中map并发读写问题及解决方法

    一.map并发读写问题 如果map由多协程同时读和写就会出现 fatal error:concurrent map read and map write的错误 如下代码很容易就出现map并发读写问题 ...

  7. 13 款最棒的 jQuery 图像 360° 旋转插件

    在 web 页面上使用 jQuery 图像 360 度旋转插件是最美也是最方便的显示图像的方式.这些超级棒的 360° 图像选择插件允许用户更详细的分析产品或者文章.jQuery 图像旋转插件可以让用 ...

  8. 关于Cocos2d-x节点和精灵节点的坐标、位置以及大小的设置

    1.cocos2d-X中的坐标(0,0),就是运行框的左下角位置,所以运行框看起来就是一个第一象限. 2.节点的锚点就是我们setPosition所设定的位置,默认锚点是在节点的中心,也就是setPo ...

  9. javascript -- 原型对象

    原型对象: 每个对象都有一个参考对象,这个参考对象称之为原型对象.原型对象有自己的属性和方法.当A是B的原型对象时,那 么B拥有A中的所有属性和方法. 原型对象的工作原理: 使用原型对象定义一个新的对 ...

  10. struct iphdr中的__LITTLE_ENDIAN_BITFIELD和__BIG_ENDIAN_BITFIELD

    __LITTLE_ENDIAN_BITFIELD表示小端序,__BIG_ENDIAN_BITFIELD表示大端序. /usr/include/linux/ip.h中有一段代码定义了ip首部的结构体,例 ...