iOS拍照後圖片自動旋轉90度的完美解決方法

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

今天開發一個拍照獲取照片的功能的時候, 發現上傳之後圖片會自動旋轉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:
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:
// Grr...
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度的完美解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對指令碼之家網站的支援!

您可能感興趣的文章:

IOS UIImagePickerController從拍照、相簿、相簿獲取圖片JS解決IOS中拍照圖片預覽旋轉90度BUG的問題Android 實現IOS選擇拍照相簿底部彈出的例項ios利用 AFN 上傳相簿或者拍照圖片詳解利用exif.js解決ios手機上傳豎拍照片旋轉90度問題iOS AVCaptureSession實現視訊錄製功能iOS視訊錄製(或選擇)壓縮及上傳功能(整理)iOS仿微信相機拍照、視訊錄製功能

相關文章

IOS開發 最新文章