iOS開發(fā)- 相機(jī)(攝像頭)獲取到的圖片自動(dòng)旋轉(zhuǎn)90度解決辦法無(wú)標(biāo)題文章

用相機(jī)拍攝出來(lái)的照片含有EXIF信息,UIImage的imageOrientation屬性指的就是EXIF中的orientation信息。

果我們忽略orientation信息,而直接對(duì)照片進(jìn)行像素處理或者drawInRect等操作,得到的結(jié)果是翻轉(zhuǎn)或者旋轉(zhuǎn)90之后的樣子。這是因?yàn)槲?/p>

們執(zhí)行像素處理或者drawInRect等操作之后,imageOrientaion信息被刪除了,imageOrientaion被重設(shè)為0,造成照片

內(nèi)容和imageOrientaion不匹配。

所以,在對(duì)照片進(jìn)行處理之前,先將照片旋轉(zhuǎn)到正確的方向,并且返回的imageOrientaion為0。

下面這個(gè)方法就是一個(gè)UIImage category中的方法,用它可以達(dá)到以上目的。

- (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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容