利用CGContext,对图片进行旋转处理并作等比缩放,保持图片的宽高比例。在CGContext中,对图片的几何处理主要用到CTM属性,该属性是坐标系对于context的映射,效果相当于对context进行了几何变换,其实只是坐标系的映射发生了几何变换而已。transalte,rotate只对原点坐标系的变换,scale则不会改变坐标系映射,但改变了点的缩放比例。
#import "RotateViewController.h"
#define HEIGHT_MAX self.view.frame.size.height-70
@interface RotateViewController ()
@end
//用于记录当前图片旋转的方向
static int i = UP;
@implementation RotateViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
//获取需要处理的图片
original = [[UIImage imageNamed:@"test.png"]retain];
NSLog(@"original:%@",NSStringFromCGSize(original.size));
//对图片进行旋转和等比缩放处理
UIImage* image = [self rotateImage];
NSLog(@"image:%@",NSStringFromCGSize(image.size));
//创建UIImageView,大小为等比处理后的图片的大小
rotateView = [[UIImageView alloc]initWithFrame:CGRectMake(0, (self.view.frame.size.height-image.size.height)/2, image.size.width, image.size.height)];
rotateView.image = image;
[self.view addSubview:rotateView];
//创建旋转操作的按钮
UIButton* rotate = [UIButton buttonWithType:UIButtonTypeCustom];
rotate.frame = CGRectMake(0, 0, 60, 30);
[rotate setTitle:@"rotate" forState:UIControlStateNormal];
[rotate setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
rotate.backgroundColor = [UIColor grayColor];
rotate.alpha = 0.5;
[rotate addTarget:self action:@selector(rotate:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:rotate];
//创建用于保存照片的按钮
UIButton* save = [UIButton buttonWithType:UIButtonTypeCustom];
save.frame = CGRectMake(self.view.frame.size.width-60, 0, 60, 30);
[save setTitle:@"save" forState:UIControlStateNormal];
[save setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
save.backgroundColor = [UIColor grayColor];
save.alpha = 0.5;
[save addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:save];
}
//旋转按钮事件
-(void)rotate:(id)sender{
//重设图片当前旋转方向
i++;
if (i==ALL) {
i=UP;
}
//获取旋转处理后的等比缩放图片
UIImage* image = [self rotateImage];
//重设UIImageView
rotateView.frame = CGRectMake(0, (self.view.frame.size.height-image.size.height)/2, image.size.width, image.size.height);
rotateView.image = image;
}
//保存按钮事件
-(void)save:(id)sender{
//保存照片到相册
UIImageWriteToSavedPhotosAlbum(rotateView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//旋转图片函数
-(UIImage*)rotateImage{
switch (i) {
//当旋转方向为正上方时
case UP:
{
//图片宽度需要填满屏幕宽度,所以选取宽度的缩放比率
double r = original.size.width/self.view.frame.size.width;
//创建cotext,大小设为宽为屏幕宽,高为原图高度等比缩放后高度
double height = 0;
if (original.size.height/r>HEIGHT_MAX) {
height = HEIGHT_MAX;
}else height = original.size.height/r;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.size.width, height), NO, 0);
[original drawInRect:CGRectMake(0, 0, self.view.frame.size.width, height)];
}
break;
//当旋转方向为顺时针90度时
case RIGHT:
{
//用原图的高度和屏幕宽度做缩放比
double r = original.size.height/self.view.frame.size.width;
//创建cotext,大小设为宽为屏幕宽,高为原图宽度等比缩放后高度
double height = 0;
if (original.size.width/r>HEIGHT_MAX) {
height = HEIGHT_MAX;
}else height = original.size.width/r;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.size.width, height), NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
//图片旋转90度,需要使用context的ctm属性,先平移再旋转。这样处理ctm后,context的坐标映射就是几何变换后的效果
CGContextTranslateCTM(context, self.view.frame.size.width, 0);
CGContextConcatCTM(context, CGAffineTransformMakeRotation(M_PI*90/180));
[original drawInRect:CGRectMake(0, 0,height,self.view.frame.size.width)];
}
break;
//当旋转方向为顺时针180度时
case DOWN:
{
//用原图的宽度和屏幕宽度做缩放比
double r = original.size.width/self.view.frame.size.width;
//创建cotext,大小设为宽为屏幕宽,高为原图高度等比缩放后高度
double height = 0;
if (original.size.height/r>HEIGHT_MAX) {
height = HEIGHT_MAX;
}else height = original.size.height/r;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.size.width, height), NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
//图片旋转180度,需要使用context的ctm属性,先平移再旋转。这样处理ctm后,context的坐标映射就是几何变换后的效果
CGContextTranslateCTM(context, self.view.frame.size.width, height);
CGContextConcatCTM(context, CGAffineTransformMakeRotation(M_PI*180/180));
[original drawInRect:CGRectMake(0, 0, self.view.frame.size.width, height)];
}
break;
//当旋转方向为顺时针270度时
case LEFT:
{
//用原图的高度和屏幕宽度做缩放比
double r = original.size.height/self.view.frame.size.width;
//创建cotext,大小设为宽为屏幕宽,高为原图宽度等比缩放后高度
double height = 0;
if (original.size.width/r>HEIGHT_MAX) {
height = HEIGHT_MAX;
}else height = original.size.width/r;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.size.width, height), NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
//图片旋转270度,需要使用context的ctm属性,先平移再旋转。这样处理ctm后,context的坐标映射就是几何变换后的效果
CGContextTranslateCTM(context,0, height);
CGContextConcatCTM(context, CGAffineTransformMakeRotation(M_PI*270/180));
[original drawInRect:CGRectMake(0, 0, height, self.view.frame.size.width)];
}
break;
default:
break;
}
//获取处理后的图片
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//照片成功保存后的回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo{
NSLog(@"saved..");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc{
[original release];
[rotateView release];
[super dealloc];
}
@end
程序效果图:
保存到相册后的效果图: