iOS -攝像頭拍攝照片自動旋轉(zhuǎn)90度處理(Swift + OC版本)

相關(guān)解釋來自這里

原文答案:
If you turned your camera properly when taking the shot, and you use fairly current photo software, rotating sideways pictures is generally not something you should have to worry about in post processing. Most modern cameras have a rotation sensor which writes an orientation EXIF tag into the file to tell your software how to turn the photo for display. You also might want to open the File preferences for Photoshop and check to see if I**gnore EXIF Profile Tag **has been selected.However, there are some situations in which your photo will not be rotated properly and will display sideways.

大致意思是說王浴,現(xiàn)在攝像頭都會有一個方向旋轉(zhuǎn)傳感器宵荒,方向信息作為EXIF信息標(biāo)記到照片當(dāng)中穆壕,憑借此方向信息系統(tǒng)會“知道”如何正確的顯示照片,當(dāng)然有一些情況下创坞,照片無法被正確旋轉(zhuǎn)或者橫向顯示。

由此可知:當(dāng)ImageView上的圖片橫向顯示或者旋轉(zhuǎn)了其他角度就說明EXIF中的方向標(biāo)簽被重置了极颓,當(dāng)代碼中有處理像素或者裁剪或者重畫圖片的功能很有可能就“抹掉”了EXIF信息中的“方向”標(biāo)簽內(nèi)對應(yīng)的消息湿痢。
對此StackOverflow中找到了OC版本的處理方法。
UIImagecategory中添加:

+ (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) {  
    case UIImageOrientationDown:  
    case UIImageOrientationDownMirrored:  
      transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);  
      transform = CGAffineTransformRotate(transform, M_PI);  
      break;  
        
    case UIImageOrientationLeft:  
    case UIImageOrientationLeftMirrored:  
      transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);  
      transform = CGAffineTransformRotate(transform, M_PI_2);  
      break;  
        
    case UIImageOrientationRight:  
    case UIImageOrientationRightMirrored:  
      transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);  
      transform = CGAffineTransformRotate(transform, -M_PI_2);  
      break;  
    default:  
      break;  
  }  
    
  switch (aImage.imageOrientation) {  
    case UIImageOrientationUpMirrored:  
    case UIImageOrientationDownMirrored:  
      transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);  
      transform = CGAffineTransformScale(transform, -1, 1);  
      break;  
        
    case UIImageOrientationLeftMirrored:  
    case UIImageOrientationRightMirrored:  
      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) {  
    case UIImageOrientationLeft:  
    case UIImageOrientationLeftMirrored:  
    case UIImageOrientationRight:  
    case UIImageOrientationRightMirrored:  
      // 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 = [UIImage imageWithCGImage:cgimg];  
  CGContextRelease(ctx);  
  CGImageRelease(cgimg);  
  return img;  
}  

自己重寫了Swift版本(此段寫于2017/02/06台囱,適用于Swift 3):
在extension中添加

class func fixOrientation(image : UIImage) -> UIImage {
        if image.imageOrientation == .up {
            return image
        }
        var transform = CGAffineTransform.identity
        switch image.imageOrientation {
        case .down,.downMirrored:
            transform = transform.translatedBy(x: image.size.width, y: image.size.height)
            transform = transform.rotated(by: CGFloat(M_PI))
        case .left,.leftMirrored:
            transform = transform.translatedBy(x: image.size.width, y: 0)
            transform = transform.rotated(by: CGFloat(M_PI_2))

        case .right,.rightMirrored:
            transform = transform.translatedBy(x: 0, y: image.size.height)
            transform = transform.rotated(by: CGFloat(-M_PI_2))
       
        default:
            break
        }
        
        
        switch image.imageOrientation {
        case .upMirrored, .downMirrored:
            transform = transform.translatedBy(x: image.size.width, y: 0)
            transform = transform.scaledBy(x: -1, y: 1)
        case .leftMirrored, .rightMirrored:
            transform = transform.translatedBy(x: image.size.height, y: 0)
            transform = transform.scaledBy(x: -1, y: 1)
        default:
            break
        }
        
        
        let ctx = CGContext(data: nil, width: Int(image.size.width), height: Int(image.size.height), bitsPerComponent: image.cgImage!.bitsPerComponent, bytesPerRow: 0, space: image.cgImage!.colorSpace!, bitmapInfo: image.cgImage!.bitmapInfo.rawValue)
        ctx!.concatenate(transform)
        switch image.imageOrientation {
        case .left,.leftMirrored,.rightMirrored,.right:
            ctx?.draw(image.cgImage!, in: CGRect(x :0,y:0,width:image.size.height,height: image.size.width))
            
        default:
             ctx?.draw(image.cgImage!, in: CGRect(x :0,y:0,width:image.size.width,height: image.size.height))
        }
        let cgimg = ctx!.makeImage()
        let img = UIImage(cgImage: cgimg!)   
        return img
    }


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末淡溯,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子簿训,更是在濱河造成了極大的恐慌咱娶,老刑警劉巖米间,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異膘侮,居然都是意外死亡屈糊,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門琼了,熙熙樓的掌柜王于貴愁眉苦臉地迎上來逻锐,“玉大人,你說我怎么就攤上這事雕薪∏ィ” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵蹦哼,是天一觀的道長鳄哭。 經(jīng)常有香客問我,道長纲熏,這世上最難降的妖魔是什么妆丘? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮局劲,結(jié)果婚禮上勺拣,老公的妹妹穿的比我還像新娘。我一直安慰自己鱼填,他們只是感情好药有,可當(dāng)我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著苹丸,像睡著了一般愤惰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上赘理,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天宦言,我揣著相機與錄音,去河邊找鬼商模。 笑死焚虱,一個胖子當(dāng)著我的面吹牛蚁吝,可吹牛的內(nèi)容都是我干的箕戳。 我是一名探鬼主播确憨,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼瞪醋!你這毒婦竟也來了忿晕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤趟章,失蹤者是張志新(化名)和其女友劉穎杏糙,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蚓土,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡宏侍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蜀漆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谅河。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖确丢,靈堂內(nèi)的尸體忽然破棺而出绷耍,到底是詐尸還是另有隱情,我是刑警寧澤鲜侥,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布褂始,位于F島的核電站,受9級特大地震影響描函,放射性物質(zhì)發(fā)生泄漏崎苗。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一舀寓、第九天 我趴在偏房一處隱蔽的房頂上張望胆数。 院中可真熱鬧,春花似錦互墓、人聲如沸必尼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽判莉。三九已至,卻和暖如春育谬,著一層夾襖步出監(jiān)牢的瞬間骂租,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工斑司, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留渗饮,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓宿刮,卻偏偏與公主長得像互站,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子僵缺,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容