欢迎使用CSDN-markdown编辑器
2016-06-22 14:36
555 查看
UIImage转90,180,270度的方法 (iOS)
转载自:ht tp://blog.sina.com.cn/s/blog_6602ffbc0101ckx3.html
这里要分享的是将image旋转,而不是将imageView旋转,原理就是使用quartz2D来画图片,然后使用ctm变幻来实现旋转。
注:quartz2D的绘图左边和oc里面的绘图左边不一样,导致绘画出的图片是反转的。所以一上来得使它转正再进行进一步的旋转等
转载自:ht tp://blog.sina.com.cn/s/blog_6602ffbc0101ckx3.html
这里要分享的是将image旋转,而不是将imageView旋转,原理就是使用quartz2D来画图片,然后使用ctm变幻来实现旋转。
注:quartz2D的绘图左边和oc里面的绘图左边不一样,导致绘画出的图片是反转的。所以一上来得使它转正再进行进一步的旋转等
+ (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation { long double rotate = 0.0; CGRect rect; float translateX = 0; float translateY = 0; float scaleX = 1.0; float scaleY = 1.0; switch (orientation) { case UIImageOrientationLeft: rotate = M_PI_2; rect = CGRectMake(0, 0, image.size.height, image.size.width); translateX = 0; translateY = -rect.size.width; scaleY = rect.size.width/rect.size.height; scaleX = rect.size.height/rect.size.width; break; case UIImageOrientationRight: rotate = 3 * M_PI_2; rect = CGRectMake(0, 0, image.size.height, image.size.width); translateX = -rect.size.height; translateY = 0; scaleY = rect.size.width/rect.size.height; scaleX = rect.size.height/rect.size.width; break; case UIImageOrientationDown: rotate = M_PI; rect = CGRectMake(0, 0, image.size.width, image.size.height); translateX = -rect.size.width; translateY = -rect.size.height; break; default: rotate = 0.0; rect = CGRectMake(0, 0, image.size.width, image.size.height); translateX = 0; translateY = 0; break; } UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); //做CTM变换 CGContextTranslateCTM(context, 0.0, rect.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGContextRotateCTM(context, rotate); CGContextTranslateCTM(context, translateX, translateY); CGContextScaleCTM(context, scaleX, scaleY); //绘制图片 CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage); UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext(); return newPic; }
相关文章推荐
- iOS _ 获取指定的UITableViewCell 并刷新
- PHP中跳出多重循环使用break,continue,goto,return,exit的用法和区别
- arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别
- 为 VR 优化 UE 4 渲染器
- 动态计算UITableViewCell高度详解 Auto Layou + Manual Layout
- 【Arduino官方教程第一辑】示例程序 6-2 用压电元件检测振动
- hibernate中调用query.list()而出现的黄色警告线
- percona-MYSQLGUI监控
- UITextView 开始编辑时,文字没有左上角对齐解决办法 tableview整体上移
- 动态计算UITableViewCell高度详解
- does not contain bitcode. You must rebuild it with
- UITableView和UITableViewCell的层级关系
- 常见MFC UI界面库
- iOS9 Storyboard unwind segue反回传递事件时机详细步骤
- iOS UI、Xcode、调试、代码等常见问题总汇(持续更新中)
- iOS import framework头文件时报错could not build module xxx
- 详解vue的数据binding原理
- PropertyAnimator学习之ValueAnimator
- Starting MySQL.. ERROR! The server quit without updating PID file
- [iOS导航] UINavgationBar导航栏隐藏