问题描述:选取相册图片或使用摄像头拍摄图片后,得到的图像旋转90度
解决方法:在获取图片,使用之前,调整图像的方向属性
结果:正常运行
相关函数:
CGAffineTransformTranslate(transform, x, y):改变位置,
CGAffineTransformMakeTranslation(width, 0.0);改变位置,
CGAffineTransformRotate(transform, M_PI);旋转。
CGAffineTransformMakeRotation(-M_PI);旋转的
transform = CGAffineTransformScale(transform, -1.0, 1.0);是缩放的。
?view.transform = CGAffineTransformIdentity;当改变过一个view.transform属性或者view.layer.transform的时候需要恢复默认状态的话,记得先把他们重置可以使用
view.transform = CGAffineTransformIdentity,
或者view.layer.transform = CATransform3DIdentity,
------------------------------------------------------------------------------------------------------------------------------------------------
扩展UIImage类,增加新方法fixOrientation以使图像回归正确的方向属性
UIImage+fixOrientation.h
#import <UIKit/UIKit.h> @interface UIImage (fixOrientation) - (UIImage *)fixOrientation; @end
UIImage+fixOrientation.m
#import "UIImage+fixOrientation.h" @implementation UIImage (fixOrientation) - (UIImage *)fixOrientation { // 如果图像已经是正确方向则不做操作 if (self.imageOrientation == UIImageOrientationUp) return self; // 否则执行对应的操作以调整方向 // 1,旋转 2,翻转 CGAffineTransform transform = CGAffineTransformIdentity; switch (self.imageOrientation) { case UIImageOrientationDown://方向向下 case UIImageOrientationDownMirrored://方向下并且被翻转 transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);//移动至右下角 transform = CGAffineTransformRotate(transform, M_PI);//以右下角为圆点旋转180度 break;//更改结果,方向朝上 case UIImageOrientationLeft://方向向左 case UIImageOrientationLeftMirrored://方向向左并且翻转 transform = CGAffineTransformTranslate(transform, self.size.width, 0);//右移width transform = CGAffineTransformRotate(transform, M_PI_2);//逆时针旋转90度 break; caseUIImageOrientationRight://方向向右 caseUIImageOrientationRightMirrored://方向向右并且翻转 transform = CGAffineTransformTranslate(transform, 0, self.size.height);//下移 transform = CGAffineTransformRotate(transform, -M_PI_2);//顺时针旋转-90度 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; }