下面是苹果官方关于UIImageOrientation的定义:
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
};
我们一一来看一下上面类型的具体意思:
1、UIImageOrientationUp:
这个很好理解,就是图片前后没有变化
2、UIImageOrientationDown:
通过上面的例子可以看到,意思是图片旋转了180度
3、UIImageOrientationLeft:
通过上面的例子可以看到,意思是图片逆时针旋转了90度
4、UIImageOrientationRight:
通过上面的例子可以看到,意思是图片顺时针旋转了90度
5、UIImageOrientationUpMirrored:
通过上面的例子可以看到,意思是对原图进行了左右镜像,即水平镜像
6、UIImageOrientationDownMirrored:
通过上面的例子可以看到,意思是对旋转了180的图片进行了左右镜像,也可以理解对原图进行了垂直镜像,既上下镜像
7、UIImageOrientationLeftMirrored:
通过上面的例子可以看到,意思是对进行逆时针旋转90度图片做了上下镜像,也可以理解为对进行顺时针旋转90度图片做了左右镜像
8、UIImageOrientationRightMirrored:
通过上面的例子可以看到,意思是对进行顺时针旋转90度图片做了上下镜像。也可以理解为对进行逆时针旋转90度图片做了左右镜像
当具有上面旋转属性的original UIImage在UIImageView上面显示的时候,会按照上面的属性做相应的旋转,这就是为什么有时候拍照的时候照片看起来方向是正确的,但拍出来的照片显示时候被旋转了90度,因为照片的方向属性是UIImageOrientationRight,这就需要对照片的方向进行纠正:
/** 纠正图片的方向 */
- (UIImage *)fixUIImageOrientation:(UIImage *)originalImage
{
if (originalImage.imageOrientation == UIImageOrientationUp) return originalImage;
// 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 (originalImage.imageOrientation)
{
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, originalImage.size.width, originalImage.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, originalImage.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, originalImage.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
}
switch (originalImage.imageOrientation)
{
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, originalImage.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, originalImage.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, originalImage.size.width, originalImage.size.height,
CGImageGetBitsPerComponent(self.CGImage), 0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
CGContextConcatCTM(ctx, transform);
switch (originalImage.imageOrientation)
{
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextDrawImage(ctx, CGRectMake(0,0,originalImage.size.height,originalImage.size.width), self.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,originalImage.size.width,originalImage.size.height), self.CGImage);
break;
}
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
网友评论