解决图库选择上传图⽚时旋转90°问题
在项⽬开发中,有时候图库选择或拍照上传图⽚时,会出现图⽚旋转 90° 的问题,
暂发现⼤于 2M 的图会出现这个问题,下⾯是解决⽅法,在上传图⽚之前进⾏图⽚
的⽅向调整即可。
//修正图⽚旋转问题
- (UIImage *)fixImageOrientation:(UIImage *)image {
//image 的 imageOrientation 属性记录有图⽚的⽅向信息
if (image.imageOrientation == UIImageOrientationUp)
return image;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, image.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
rotate属性default:
break;
}
switch (image.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, image.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, image.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
default:
break;
}
CGContextRef contextRef = CGBitmapContextCreate(NULL, image.size.width, image.size.height, C
GImageGetBitsPerComponent(image.CGImage), 0, CGImageGetColor CGContextConcatCTM(contextRef, transform);
switch (image.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextDrawImage(contextRef, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
break;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论