零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 基础
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 线程
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> OpenGL ES
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> GPUImage
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> AVFoundation
零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> CocoaPods
一.UIImageOrientation 简介
UIImageOrientation 枚举是 UIKit 封装好的,分别是向上,向下,向左,向右等等,后面的一半不常用
typedef NS_ENUM(NSInteger, UIImageOrientation) {
UIImageOrientationUp, // default orientation
UIImageOrientationDown, // 180 deg rotation
UIImageOrientationLeft, // 90 deg CCW
UIImageOrientationRight, // 90 deg CW
UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip
UIImageOrientationDownMirrored, // horizontal flip
UIImageOrientationLeftMirrored, // vertical flip
UIImageOrientationRightMirrored, // vertical flip
}
二.UIImage 旋转
/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:Object-C UIImage 旋转
//@Time:2021/09/28 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/
#pragma mark - 旋转 UIImage
/**
* return 旋转后的图片
* @param image 原始图片 (必传,不可空)
* @param orientation 旋转方向 (必传,不可空)
*/
-(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 = 33 * 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;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIImage* srcImage = [UIImage imageNamed:@"123.png"];
UIImage* dstImage = [self image:srcImage rotation:UIImageOrientationDown];
NSLog(@"src width:%f height:%f",srcImage.size.width,srcImage.size.height);
NSLog(@"dst width:%f height:%f",dstImage.size.width,dstImage.size.height);
}
/*
src width:503.000000 height:366.000000
dst width:503.000000 height:366.000000
*/
原图
UIImageOrientationDown
UIImageOrientationLeft
三.猜你喜欢
- iOS NSString 拆分字符串
- iOS NSString 截取字符串
- iOS NSString 和 NSArray 相互转换
- iOS NSString 包含字符串/匹配字符串
- iOS NSURL URLWithString 与 fileURLWithPath 区别
- iOS NSURL 与 NSString相互转换
- iOS NSString 获取中文字符串长度
- iOS NSString 判断字符串是否为空判
- iOS NSString 获取指定下标字符
- iOS NSString 匹配字符串开头
- iOS NSString 匹配字符串结尾
- iOS NSString 判断两个字符串是否相等
- iOS NSString 将字符串中的字母转换为大写
- iOS NSString 将字符串中的字母转换为小写
- iOS NSString 将字符串中的首字母变为大写
- iOS NSString 拼接字符串
- iOS NSString 插入字符串
- iOS NSString 删除字符串
- iOS NSNumber 和 int 相互转换
- iOS NSNumber compare 比较
- iOS NSNumber isEqualToNumber
- iOS NSValue 和 CGPoint 相互转换
- iOS NSValue 和 CGSize 相互转换
- iOS UIImage 与 RGBA 相互转换
- Object-C UIImage 缩放
- Object-C UIImage 裁剪
- Object-C UIImage 旋转
ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容