博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS - UIImageView - how to handle UIImage image orientation
阅读量:7071 次
发布时间:2019-06-28

本文共 2905 字,大约阅读时间需要 9 分钟。

本文转载至 http://stackoverflow.com/questions/8915630/ios-uiimageview-how-to-handle-uiimage-image-orientation
 
 
 
 
18
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

 

4 Answers

54accepted

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.

 
1  
By the way, how can I actually rotate the image data? –   
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. –   
    
Thanks, I've got it over! –   
20

As Anomie suggests in their answer , 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:
你可能感兴趣的文章
关于.net和java我的见解
查看>>
【Android】设置Dialog点击屏幕不消失
查看>>
ConcurrentDictionary与Dictionary
查看>>
Atom Remote-FTP connecting FTP with SSL/TLS
查看>>
《代码大全》阅读笔记-27-程序规模对构建的影响
查看>>
What is R语言
查看>>
【给你一个承诺 - 玩转 AngularJS 的 Promise】
查看>>
P4962 朋也与光玉
查看>>
关于flash cs4意外退出的问题
查看>>
一道笔试指针题目详解
查看>>
easyui datagrid 绑定从后台得到的复杂的特殊数据结构
查看>>
makefile 字符串处理函数
查看>>
Class Prefix(Xcode6以后设置类前缀)
查看>>
(转载)创业型公司如何管理-吸引人才
查看>>
Oracle Spool教程
查看>>
通过jQuery实现的百叶窗效果
查看>>
设置文本显示为2行,溢出隐藏后以省略号结尾
查看>>
Sequelize-nodejs-13-Working with legacy tables
查看>>
virtualbox+vagrant学习-2(command cli)-1-vagrant box命令
查看>>
数据库相关
查看>>