首页 > 代码库 > UIImage 旋转和保存旋转 (转载)
UIImage 旋转和保存旋转 (转载)
最近一直没有写笔记, 总感觉自己少了点什么, 也没有转载什么有用的笔记, 今天小弟特此献上一篇图片旋转和保存旋转的功能实现, 注意这是转载, 我是来当自己笔记的, 哈哈哈哈...
1 @interface ViewController () 2 { 3 UIImageView * iv; 4 } 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad10 {11 [super viewDidLoad];12 // Do any additional setup after loading the view, typically from a nib.13 14 CGRect rect = [[UIScreen mainScreen] bounds];15 16 // 图像17 iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"haha.png"]];18 iv.frame = CGRectMake(60, 100, 200, 200);19 [self.view addSubview:iv];20 21 // 旋转按扭22 UIButton * rotateBtn = [UIButton buttonWithType:UIButtonTypeCustom];23 rotateBtn.frame = CGRectMake(0, rect.size.height-44, rect.size.width, 44);24 rotateBtn.backgroundColor = [UIColor greenColor];25 [rotateBtn addTarget:self action:@selector(rotateBtnClick) forControlEvents:UIControlEventTouchUpInside];26 [self.view addSubview:rotateBtn];27 }28 29 #pragma mark - 调用图片旋转方法并保存旋转30 - (void)rotateBtnClick31 {32 iv.image = [self image:iv.image rotation:UIImageOrientationRight];33 34 NSString * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];35 NSString * filePath = [NSString stringWithFormat:@"%@/rotateImage.png", path];36 NSData * data =http://www.mamicode.com/ UIImagePNGRepresentation(iv.image);37 [data writeToFile:filePath atomically:YES];38 }39 40 #pragma mark - 图片旋转方法41 - (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation42 {43 long double rotate = 0.0;44 CGRect rect;45 float translateX = 0;46 float translateY = 0;47 float scaleX = 1.0;48 float scaleY = 1.0;49 50 switch (orientation) {51 case UIImageOrientationLeft:52 rotate = M_PI_2;53 rect = CGRectMake(0, 0, image.size.height, image.size.width);54 translateX = 0;55 translateY = -rect.size.width;56 scaleY = rect.size.width/rect.size.height;57 scaleX = rect.size.height/rect.size.width;58 break;59 case UIImageOrientationRight:60 rotate = 3 * M_PI_2;61 rect = CGRectMake(0, 0, image.size.height, image.size.width);62 translateX = -rect.size.height;63 translateY = 0;64 scaleY = rect.size.width/rect.size.height;65 scaleX = rect.size.height/rect.size.width;66 break;67 case UIImageOrientationDown:68 rotate = M_PI;69 rect = CGRectMake(0, 0, image.size.width, image.size.height);70 translateX = -rect.size.width;71 translateY = -rect.size.height;72 break;73 default:74 rotate = 0.0;75 rect = CGRectMake(0, 0, image.size.width, image.size.height);76 translateX = 0;77 translateY = 0;78 break;79 }80 81 UIGraphicsBeginImageContext(rect.size);82 CGContextRef context = UIGraphicsGetCurrentContext();83 84 //做CTM变换85 CGContextTranslateCTM(context, 0.0, rect.size.height);86 CGContextScaleCTM(context, 1.0, -1.0);87 CGContextRotateCTM(context, rotate);88 CGContextTranslateCTM(context, translateX, translateY);89 CGContextScaleCTM(context, scaleX, scaleY);90 91 //绘制图片92 CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);93 94 UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();95 96 return newPic;97 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。