标签:style blog color width for io
最近一直没有写笔记, 总感觉自己少了点什么, 也没有转载什么有用的笔记, 今天小弟特此献上一篇图片旋转和保存旋转的功能实现, 注意这是转载, 我是来当自己笔记的, 哈哈哈哈...
1 @interface ViewController () 2 { 3 UIImageView * iv; 4 } 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad 10 { 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)rotateBtnClick 31 { 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 = UIImagePNGRepresentation(iv.image); 37 [data writeToFile:filePath atomically:YES]; 38 } 39 40 #pragma mark - 图片旋转方法 41 - (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation 42 { 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 }
UIImage 旋转和保存旋转 (转载),布布扣,bubuko.com
标签:style blog color width for io
原文地址:http://www.cnblogs.com/ZeroHour/p/3845162.html