欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年12月24日 10:01:39   作者:sharpyl  
今天開發(fā)一個拍照獲取照片的功能的時候, 發(fā)現(xiàn)上傳之后圖片會自動旋轉90.在測試中發(fā)現(xiàn)只要是圖片大于2M, 系統(tǒng)就會自動翻轉照片。下面小編通過本文給大家分享下解決辦法

今天開發(fā)一個拍照獲取照片的功能的時候, 發(fā)現(xiàn)上傳之后圖片會自動旋轉90.

測試發(fā)現(xiàn), 只要是圖片大于2M, 系統(tǒng)就會自動翻轉照片

  相機拍照后直接取出來的UIimage(用UIImagePickerControllerOriginalImage取出),它本身的imageOrientation屬性是3,即UIImageOrientationRight。如果這個圖片直接使用則沒事,但是如果對它進行裁剪、縮放等操作后,它的這個imageOrientation屬性會變成0。此時這張圖片用在別的地方就會發(fā)生旋轉。imageOrientation是只讀的,不能直接修改其值。

解決方法如下:

1. 設置相機的一個屬性allowsEditing為YES,設了這個值,你拍完照片后會在照片上出現(xiàn)一個框框,這就是對照片的裁剪編輯。在相機的代理方法中取照片的時候就別用UIImagePickerControllerOriginalImage來取了,要用UIImagePickerControllerEditedImage。用這個key取出來的照片,它的imageOrientation是0,所以之后的任何裁剪、縮放操作都不會造成旋轉。這是第一種方法。

2. 第一種解決方法基本沒用, 開發(fā)中基本都會對圖片進行裁剪和壓縮. 這里有一個專門針對這個事的很好的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度的完美解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

  • iOS?xcconfig編寫示例教程

    iOS?xcconfig編寫示例教程

    這篇文章主要為大家介紹了iOS?xcconfig編寫示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • 實例解析設計模式中的外觀模式在iOS App開發(fā)中的運用

    實例解析設計模式中的外觀模式在iOS App開發(fā)中的運用

    這篇文章主要介紹了設計模式中的外觀模式在iOS App開發(fā)中的運用,實例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-03-03
  • 清除WKWebView cookies的方法

    清除WKWebView cookies的方法

    下面小編就為大家?guī)硪黄宄齏KWebView cookies的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • iOS實現(xiàn)萌貨貓頭鷹登錄界面動畫

    iOS實現(xiàn)萌貨貓頭鷹登錄界面動畫

    本文介紹的動畫效果仿自國外網(wǎng)站readme.io的登錄界面,超萌可愛的貓頭鷹,感興趣的朋友們可以參考學習。
    2016-08-08
  • IOS代碼筆記之網(wǎng)絡嗅探功能

    IOS代碼筆記之網(wǎng)絡嗅探功能

    這篇文章主要為大家詳細介紹了IOS網(wǎng)絡嗅探功能實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • iOS快速實現(xiàn)環(huán)形漸變進度條

    iOS快速實現(xiàn)環(huán)形漸變進度條

    之前看到很多環(huán)形進度條,看上去很酷,然后就試著學習他們的代碼,結果發(fā)現(xiàn)實現(xiàn)一個環(huán)形進度條一點也不簡單。我就在想一個簡單的進度條有這么復雜嗎?自己摸索后,有一個簡單的實現(xiàn)方法?,F(xiàn)在分享給大家,有需要的朋友們可以參考借鑒。
    2016-10-10
  • iOS利用AFNetworking實現(xiàn)文件上傳的示例代碼

    iOS利用AFNetworking實現(xiàn)文件上傳的示例代碼

    本篇文章主要介紹了iOS利用AFNetworking實現(xiàn)文件上傳的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • iOS多線程實現(xiàn)多圖下載功能

    iOS多線程實現(xiàn)多圖下載功能

    這篇文章主要為大家詳細介紹了iOS多線程實現(xiàn)多圖下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • iOS  隱藏導航條和狀態(tài)欄實現(xiàn)方法

    iOS 隱藏導航條和狀態(tài)欄實現(xiàn)方法

    這篇文章主要介紹了 iOS隱藏導航條和狀態(tài)欄實現(xiàn)方法的相關資料,有時候根據(jù)需求開發(fā)APP 需要隱藏導航欄和狀態(tài)欄,這里提供了實現(xiàn)方法需要的朋友可以參考下
    2016-11-11
  • iOS tableView實現(xiàn)下拉圖片放大效果

    iOS tableView實現(xiàn)下拉圖片放大效果

    這篇文章主要為大家詳細介紹了iOS tableView實現(xiàn)下拉圖片放大效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05

最新評論