你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS拍照後圖片自動旋轉90度的完美解決方法

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

編輯:IOS開發綜合

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

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved