本文讲解UIImage的旋转方法,是UIImage,不是UIImageView!!!
主要用途:手机横屏拍照,需要得到一张width大于height的照片。或者其他用途
说实在话,图片旋转的逻辑还是很烧脑的,需要一定的空间想象(虽然只是平面旋转)。经过一段时间的艰苦奋战,终于知其一二,并总结于此,望有助于后面的人!!!
先来一段标准的图片旋转代码:
- (UIImage *)fixOrientation {
// No-op if the orientation is already correct
if (self.imageOrientation == UIImageOrientationUp) return self;
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;
switch (self.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, self.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
}
switch (self.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, self.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
CGImageGetBitsPerComponent(self.CGImage), 0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
CGContextConcatCTM(ctx, transform);
switch (self.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
break;
}
// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
imageOrientation
它是这样的:
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
};
特备注意:UIImageOrientationUp指的是手机横屏看图,也就是屏幕正对自己,home键在右边(不是竖着看手机UIDeviceOrientation相差90°)。这也是默认值。
CGAffineTransform (形变属性)
矩阵操作相对比较麻烦,事实上iOS已经为我们准备好了三个方法:
1.CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)(平移:设置平移量)
特别注意,这个平移的坐标是自然坐标,向左是X+, 向上是Y+
CGAffineTransformTranslate(transform, 100, 200);//向左移动100,向上移动200
2.CGAffineTransformMakeScale(CGFloatCGFloat sy)(缩放:设置缩放比例)仅通过设置缩放比例就可实现视图镜像翻转而来和缩进频幕的效果。
transform = CGAffineTransformScale(transform, -1, 1);//左右翻转,以Y轴为旋转轴
transform = CGAffineTransformScale(transform, 1, -1);//上下翻转,以X轴为旋转轴
3.CGAffineTransformMakeRotation(CGFloat angle)(旋转:设置旋转角度)
以上3个都是针对视图的原定最初位置的左下角为起始参照进行相应操作的,在操作结束之后可对设置量进行还原:
view.transform=CGAffineTransformIdentity;
CGBitmapContextCreate
函数原型:
CGContextRef CGBitmapContextCreate (
void *data,
size_t width,
size_t height,
size_t bitsPerComponent,
size_t bytesPerRow,
CGColorSpaceRef colorspace,
CGBitmapInfo bitmapInfo
);
参数:
data 指向要渲染的绘制内存的地址。这个内存块的大小至少是(bytesPerRow*height)个字节
width bitmap的宽度,单位为像素
height bitmap的高度,单位为像素
bitsPerComponent 内存中像素的每个组件的位数.例如,对于32位像素格式和RGB 颜色空间,你应该将这个值设为8.
bytesPerRow bitmap的每一行在内存所占的比特数
colorspace bitmap上下文使用的颜色空间。
bitmapInfo 指定bitmap是否包含alpha通道,像素中alpha通道的相对位置,像素组件是整形还是浮点型等信息的字符串。
描述:
当你调用这个函数的时候,Quartz创建一个位图绘制环境,也就是位图上下文。当你向上下文中绘制信息时,Quartz把你要绘制的信息作为位图数据绘制到指定的内存块。一个新的位图上下文的像素格式由三个参数决定:每个组件的位数,颜色空间,alpha选项。alpha值决定了绘制像素的透明性。
CGContextConcatCTM(ctx, transform)
把位图上下文ctx按照转换规则transform进行CTM转换
注:以上就是我对这些知识点的理解,关于- (UIImage *)fixOrientation{}后半部分所调用的方法都是Quartz2D的知识点。