? ? ? ? ? ? ? ? ? ? ? ? ?
下面是蘋果官方關(guān)于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
};
我們一一來(lái)看一下上面類型的具體意思:
1站绪、UIImageOrientationUp:
這個(gè)很好理解,就是圖片前后沒(méi)有變化
2尚卫、UIImageOrientationDown:
通過(guò)上面的例子可以看到,意思是圖片旋轉(zhuǎn)了180度
3方灾、UIImageOrientationLeft:
通過(guò)上面的例子可以看到称勋,意思是圖片逆時(shí)針旋轉(zhuǎn)了90度
4、UIImageOrientationRight:
通過(guò)上面的例子可以看到柬批,意思是圖片順時(shí)針旋轉(zhuǎn)了90度
5顷扩、UIImageOrientationUpMirrored:
通過(guò)上面的例子可以看到拐邪,意思是對(duì)原圖進(jìn)行了左右鏡像,即水平鏡像
6隘截、UIImageOrientationDownMirrored:
通過(guò)上面的例子可以看到扎阶,意思是對(duì)旋轉(zhuǎn)了180的圖片進(jìn)行了左右鏡像,也可以理解對(duì)原圖進(jìn)行了垂直鏡像婶芭,既上下鏡像
7东臀、UIImageOrientationLeftMirrored:
通過(guò)上面的例子可以看到,意思是對(duì)進(jìn)行逆時(shí)針旋轉(zhuǎn)90度圖片做了上下鏡像犀农,也可以理解為對(duì)進(jìn)行順時(shí)針旋轉(zhuǎn)90度圖片做了左右鏡像
8惰赋、UIImageOrientationRightMirrored:
通過(guò)上面的例子可以看到,意思是對(duì)進(jìn)行順時(shí)針旋轉(zhuǎn)90度圖片做了上下鏡像呵哨。也可以理解為對(duì)進(jìn)行逆時(shí)針旋轉(zhuǎn)90度圖片做了左右鏡像
當(dāng)具有上面旋轉(zhuǎn)屬性的original UIImage在UIImageView上面顯示的時(shí)候赁濒,會(huì)按照上面的屬性做相應(yīng)的旋轉(zhuǎn),這就是為什么有時(shí)候拍照的時(shí)候照片看起來(lái)方向是正確的孟害,但拍出來(lái)的照片顯示時(shí)候被旋轉(zhuǎn)了90度拒炎,因?yàn)檎掌姆较驅(qū)傩允荱IImageOrientationRight,這就需要對(duì)照片的方向進(jìn)行糾正:
/** 糾正圖片的方向 */
- (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;
}