iOS UIImage 圖片旋轉

最近因項目中缺少ios開發(fā),臨時客串解決一些ios問題。
在一個需求中,獲取到照片的數(shù)據,將UIImage寫入文件,發(fā)現(xiàn)生成的圖片是斜的。
朝向向右,而不是向上。
而用UIImageView時能顯示正常,因為UIImageView會根據圖片旋轉容器的方向。
而我們是要把圖片上傳到服務器,所以這個不行。

然后在網上找到的辦法如下:(不行)


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

}

該方法不行,在斷點調試時發(fā)現(xiàn),是因為該圖片UIImage的朝向就是 UIImageOrientationUp。
也就是在已進入這個方法時就return了,下面的都沒有執(zhí)行。
但我們視覺看到的確實是朝向不對,代碼檢測的卻是朝向上。

所以導致該方法不行,我試過去屏蔽掉第一句的判斷以及return。但是生成的圖片白屏。

還是過其他的辦法,發(fā)現(xiàn)有的是能旋轉過來,但圖片的寬高會有問題,導致圖片在寬度上被拉伸,高度上被壓縮,
即圖片的寬高沒有對換。

然后發(fā)現(xiàn)一個方法: quartz2D來畫圖片,然后使用ctm變幻來實現(xiàn)旋轉

原文地址:https://blog.csdn.net/bitcser/article/details/52055442?utm_source=blogxgwz4

代碼如下:

+(UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation

{

    long double rotate = 0.0;

    CGRect rect;

    float translateX = 0;

    float translateY = 0;

    float scaleX = 1.0;

    float scaleY = 1.0;

    

    switch (orientation) {

      case UIImageOrientationLeft:

           rotate =M_PI_2;

           rect =CGRectMake(0,0,image.size.height, image.size.width);

           translateX=0;

           translateY= -rect.size.width;

           scaleY =rect.size.width/rect.size.height;

           scaleX =rect.size.height/rect.size.width;

          break;

      case UIImageOrientationRight:

           rotate =3 *M_PI_2;

           rect =CGRectMake(0,0,image.size.height, image.size.width);

           translateX= -rect.size.height;

           translateY=0;

           scaleY =rect.size.width/rect.size.height;

           scaleX =rect.size.height/rect.size.width;

          break;

      case UIImageOrientationDown:

           rotate =M_PI;

           rect =CGRectMake(0,0,image.size.width, image.size.height);

           translateX= -rect.size.width;

           translateY= -rect.size.height;

          break;

      default:

           rotate =0.0;

           rect =CGRectMake(0,0,image.size.width, image.size.height);

           translateX=0;

           translateY=0;

          break;

    }

    

   UIGraphicsBeginImageContext(rect.size);

  CGContextRef context =UIGraphicsGetCurrentContext();

   //做CTM變換

    CGContextTranslateCTM(context, 0.0, rect.size.height);

    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextRotateCTM(context, rotate);

    CGContextTranslateCTM(context, translateX,translateY);

    

    CGContextScaleCTM(context, scaleX,scaleY);

   //繪制圖片

    CGContextDrawImage(context, CGRectMake(0,0,rect.size.width, rect.size.height), image.CGImage);

    

  UIImage *newPic =UIGraphicsGetImageFromCurrentImageContext();

    

    return newPic;

}


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

友情鏈接更多精彩內容