iOS - UIImageView - how to handle UIImage image orientation

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


up vote18down votefavorite

12

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

ios uiimageview uiimage orientation


shareimprove this question

asked Jan 18 ‘12 at 18:49

Martin Pilch
1,14021441

 

4 Answers

activeoldestvotes


up vote54down voteaccepted

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.


shareimprove this answer

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

add a comment


up vote20down vote

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;

}

shareimprove this answer

edited Sep 3 ‘13 at 2:00

Brad Larson
136k31307465

answered Feb 23 ‘13 at 10:44

suvish valsan
556621

 

14  

and seems to have been copied from stackoverflow.com/questions/5427656/… –  Geoff Hackworth May 27 ‘13 at 8:34
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

add a comment


up vote9down vote

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.


shareimprove this answer

answered Jul 15 ‘14 at 23:57

user2067021
1,028916

 
add a comment

up vote4down vote

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;
}

shareimprove this answer
时间: 2024-10-17 04:52:53

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

[iOS]UIImageView增加圆角

[iOS]UIImageView增加圆角 "如何给一个UIImageView增加圆角?有几种方法?各自区别?" 备注:本文参考自http://www.jianshu.com/p/d1954c9a4426 UIImageView *poImgView = [[UIImageView alloc]init]; 方案A(基本方案): poImgView.layer.cornerRadius = poImgView.frame.size.width/2.0; poImgView.layer.m

IOS将UIView转化为UIImage

+(UIImage*)createImageFromView:(UIView*)view { //obtain scale CGFloat scale = [UIScreen mainScreen].scale; 开始绘图,下面方法,第一个参数表示区域大小.第二个参数表示是否是非透明的.如果需要显示半透明效果,需要传NO,否则传YES.第三个参数就是屏幕密度了 UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.frame.size.wi

iOS图像处理(一)UIImage创建图像

原文链接: iOS图像处理(一)UIImage创建图像 简书主页:http://www.jianshu.com/users/37f2920f6848 Github主页:https://github.com/MajorLMJ iOS开发者公会-技术1群 QQ群号:87440292 iOS开发者公会-技术2群 QQ群号:232702419 iOS开发者公会-议事区   QQ群号:413102158

iOS UIImageView设置为圆形

UIImageView设置为圆形的方法: _Image.layer.masksToBounds = YES; _Image.layer.cornerRadius = self.Image.frame.size.width / 2; 设置加载本地图片的方法: _Image.image = [UIImage imageNamed:@"Image"];//图片“Image”为xcassets里的set名称

iOS:UIImageView图像视图控件

UIImageView:图像视图控件: 它是UIView的子类,因此也是视图控件,可以用来显示图像.因为它具有帧动画属性和操作方法,因此可以用来制作动画,其实动画就是很短的时间内,执行显示连续的很多张图片,人肉眼无法处分,使人看起来仿佛图像在动似的.例如典型的实例:汤姆猫实例 @interface UIImageView : UIView { @property(nonatomic,retain) UIImage *image;    //图像 @property(nonatomic,retai

ios UIImageView异步加载网络图片

方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; NSURL *photourl = [NSURL URLWithString:@"http://www.exampleforphoto.com/pabb/test32.png"]; //url请求实在UI主线程中进行的 UIImage *images = [UIImage ima

ios UIImageView处理图片大小问题

UIImageView视图可以显示图片 实例化UIImageView有两种方法 第一种方法: UIImageView *myImageView = [[ UIImageView alloc] initWithImage: [UIImage imageNamed: @"demo"]]; 用该方法可以显示图片原有大小. 第二种方法: UIImageView *myImageView = [[UIImage alloc] initWithFrame: self.view.bounds] 该方

iOS 导航栏黑线,UIImage 枚举处理方式

ios 找出导航栏下面的黑线(可隐藏,改变样式等) http://www.jianshu.com/p/effa4a48f1e3 设置UIImage的渲染模式:UIImage.renderingMode http://blog.csdn.net/djxiaoyu_haha/article/details/40949083 着色(Tint Color)是iOS7界面中的一个.设置UIImage的渲染模式:UIImage.renderingMode重大改变,你可以设置一个UIImage在渲染时是否使用

从零开始学习ios(UIImageView)控件及其属性

//创建图片视图时就设定Frame的属性和大小 UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(60, 20, 80, 100)]; [img setImage:[UIImage imageNamed:@"mtxx6"]]; /* 创建图片视图的另外四种方法 type img{ UIImageView *img1 = [[UIImageView alloc]init]; UIImageView *img2