iOS拍照后图⽚⾃动旋转90度的完美解决⽅法
今天开发⼀个拍照获取照⽚的功能的时候, 发现上传之后图⽚会⾃动旋转90.
测试发现, 只要是图⽚⼤于2M, 系统就会⾃动翻转照⽚
相机拍照后直接取出来的UIimage(⽤UIImagePickerControllerOriginalImage取出),它本⾝的imageOrientation属性是3,即UIImageOrientationRight。如果这个图⽚直接使⽤则没事,但是如果对它进⾏裁剪、缩放等操作后,它的这个imageOrientation属性会变成0。此时这张图⽚⽤在别的地⽅就会发⽣旋转。imageOrientation是只读的,不能直接修改其值。
解决⽅法如下:
1. 设置相机的⼀个属性allowsEditing为YES,设了这个值,你拍完照⽚后会在照⽚上出现⼀个框框,这就是对照⽚的裁剪编辑。在相机的代理⽅法中取照⽚的时候就别⽤UIImagePickerControllerOriginalImage来取了,要⽤UIImagePickerControllerEditedImage。⽤这个key取出来的照⽚,它的imageOrientation是0,所以之后的任何裁剪、缩放操作都不会造成旋转。这是第⼀种⽅法。
2. 第⼀种解决⽅法基本没⽤, 开发中基本都会对图⽚进⾏裁剪和压缩. 这⾥有⼀个专门针对这个事的很好的category
+ (UIImage *)fixOrientation:(UIImage *)aImage {
// No-op if the orientation is already correct
if (aImage.imageOrientation ==UIImageOrientationUp)
return aImage;
// 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 (aImage.imageOrientation) {
caseUIImageOrientationDown:
caseUIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
caseUIImageOrientationLeft:
caseUIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width,0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
caseUIImageOrientationRight:
transform的例句caseUIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch (aImage.imageOrientation) {
caseUIImageOrientationUpMirrored:
caseUIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width,0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
caseUIImageOrientationLeftMirrored:
caseUIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.height,0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
default:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx =CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage),0,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (aImage.imageOrientation) {
caseUIImageOrientationLeft:
caseUIImageOrientationLeftMirrored:
caseUIImageOrientationRight:
caseUIImageOrientationRightMirrored:
//
CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
break;
default:
CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
break;
}
// And now we just create a new UIImage from the drawing context
CGImageRef cgimg =CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImageimageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
以上所述是⼩编给⼤家介绍的iOS拍照后图⽚⾃动旋转90度的完美解决⽅法,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。