iOS開(kāi)發(fā),解決上傳照片按原圖旋轉(zhuǎn)了90度的問(wèn)題

做項(xiàng)目的時(shí)候發(fā)現(xiàn), 如果把通過(guò)相機(jī)獲取到的圖片,直接進(jìn)行操作, 比如裁剪, 縮放, 可能會(huì)把原圖片向右旋轉(zhuǎn)90度。
用相機(jī)拍攝出來(lái)的照片含有EXIF信息,UIImage的imageOrientation屬性指的就是EXIF中的orientation信息。
如果我們忽略orientation信息,而直接對(duì)照片進(jìn)行像素處理或者drawInRect等操作,得到的結(jié)果是翻轉(zhuǎn)或者旋轉(zhuǎn)90之后的樣子。這是因?yàn)槲覀儓?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;  
}

但有時(shí)候我們獲取圖片時(shí)用的是AGImagePickerController,在其從ALAsset轉(zhuǎn)為UIImage的時(shí)候可能原圖就發(fā)生了旋轉(zhuǎn),因此,這里也給出了一個(gè)處理辦法:

            for (ALAsset * asset in self.assetArray) {
                CGImageRef ref = [[asset defaultRepresentation]fullResolutionImage];
                UIImage *orgImage = [UIImage imageWithCGImage:[asset.defaultRepresentation fullScreenImage]
                                                        scale:[asset.defaultRepresentation scale] orientation:
                                     (UIImageOrientation)[asset.defaultRepresentation orientation]];
                // 這句解決了問(wèn)題
                orgImage = [UIImage imageWithCGImage:ref scale:1 orientation:orgImage.imageOrientation];
//                XMLog(@"原始選完imageOrientation===%ld",orgImage.imageOrientation);
                // 讓圖片的旋轉(zhuǎn)方向向上(即正確的方向)
                orgImage = [self fixOrientation: orgImage];
                [self.previewImageArray addObject:orgImage];
            }
            
            [delegate imagePickerDidSelectImagearray:self.previewImageArray];

但如果一開(kāi)始用以下這種寫(xiě)法從ALAsset轉(zhuǎn)為UIImage,就不會(huì)發(fā)生旋轉(zhuǎn)的問(wèn)題:

    ALAssetRepresentation *assetRep = [asset defaultRepresentation];
                CGImageRef imgRef = [assetRep fullResolutionImage];
                UIImage *orgImage = [UIImage imageWithCGImage:imgRef
                                                   scale:assetRep.scale
                                             orientation:(UIImageOrientation)assetRep.orientation];

參考資料:資料1 , 資料2 。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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