iOS拍照后圖片自動旋轉(zhuǎn)90度的完美解決方法
今天開發(fā)一個拍照獲取照片的功能的時候, 發(fā)現(xiàn)上傳之后圖片會自動旋轉(zhuǎn)90.
測試發(fā)現(xiàn), 只要是圖片大于2M, 系統(tǒng)就會自動翻轉(zhuǎn)照片
相機(jī)拍照后直接取出來的UIimage(用UIImagePickerControllerOriginalImage取出),它本身的imageOrientation屬性是3,即UIImageOrientationRight。如果這個圖片直接使用則沒事,但是如果對它進(jìn)行裁剪、縮放等操作后,它的這個imageOrientation屬性會變成0。此時這張圖片用在別的地方就會發(fā)生旋轉(zhuǎn)。imageOrientation是只讀的,不能直接修改其值。
解決方法如下:
1. 設(shè)置相機(jī)的一個屬性allowsEditing為YES,設(shè)了這個值,你拍完照片后會在照片上出現(xiàn)一個框框,這就是對照片的裁剪編輯。在相機(jī)的代理方法中取照片的時候就別用UIImagePickerControllerOriginalImage
來取了,要用UIImagePickerControllerEditedImage
。用這個key取出來的照片,它的imageOrientation是0,所以之后的任何裁剪、縮放操作都不會造成旋轉(zhuǎn)。這是第一種方法。
2. 第一種解決方法基本沒用, 開發(fā)中基本都會對圖片進(jìn)行裁剪和壓縮. 這里有一個專門針對這個事的很好的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拍照后圖片自動旋轉(zhuǎn)90度的完美解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- ios電子書翻頁效果代碼詳解
- iOS實(shí)現(xiàn)翻頁效果動畫實(shí)例代碼
- iOS圖片界面翻頁切換效果
- iOS實(shí)現(xiàn)日歷翻頁動畫
- 實(shí)例講解iOS中的UIPageViewController翻頁視圖控制器
- iOS開發(fā)中使用屏幕旋轉(zhuǎn)功能的相關(guān)方法
- iOS開發(fā)中控制屏幕旋轉(zhuǎn)的編寫方法小結(jié)
- IOS手勢操作(拖動、捏合、旋轉(zhuǎn)、點(diǎn)按、長按、輕掃、自定義)
- iOS漸變圓環(huán)旋轉(zhuǎn)動畫CAShapeLayer CAGradientLayer
- iOS基于CATransition實(shí)現(xiàn)翻頁、旋轉(zhuǎn)等動畫效果
相關(guān)文章
實(shí)例解析設(shè)計模式中的外觀模式在iOS App開發(fā)中的運(yùn)用
這篇文章主要介紹了設(shè)計模式中的外觀模式在iOS App開發(fā)中的運(yùn)用,實(shí)例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03iOS快速實(shí)現(xiàn)環(huán)形漸變進(jìn)度條
之前看到很多環(huán)形進(jìn)度條,看上去很酷,然后就試著學(xué)習(xí)他們的代碼,結(jié)果發(fā)現(xiàn)實(shí)現(xiàn)一個環(huán)形進(jìn)度條一點(diǎn)也不簡單。我就在想一個簡單的進(jìn)度條有這么復(fù)雜嗎?自己摸索后,有一個簡單的實(shí)現(xiàn)方法。現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒。2016-10-10iOS利用AFNetworking實(shí)現(xiàn)文件上傳的示例代碼
本篇文章主要介紹了iOS利用AFNetworking實(shí)現(xiàn)文件上傳的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02iOS 隱藏導(dǎo)航條和狀態(tài)欄實(shí)現(xiàn)方法
這篇文章主要介紹了 iOS隱藏導(dǎo)航條和狀態(tài)欄實(shí)現(xiàn)方法的相關(guān)資料,有時候根據(jù)需求開發(fā)APP 需要隱藏導(dǎo)航欄和狀態(tài)欄,這里提供了實(shí)現(xiàn)方法需要的朋友可以參考下2016-11-11iOS tableView實(shí)現(xiàn)下拉圖片放大效果
這篇文章主要為大家詳細(xì)介紹了iOS tableView實(shí)現(xiàn)下拉圖片放大效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05