iOS UIImage 圖片旋轉(zhuǎn)

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

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


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

}

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

所以導(dǎo)致該方法不行嘉汰,我試過去屏蔽掉第一句的判斷以及return丹禀。但是生成的圖片白屏。

還是過其他的辦法鞋怀,發(fā)現(xiàn)有的是能旋轉(zhuǎn)過來双泪,但圖片的寬高會有問題,導(dǎo)致圖片在寬度上被拉伸密似,高度上被壓縮焙矛,
即圖片的寬高沒有對換。

然后發(fā)現(xiàn)一個方法: quartz2D來畫圖片辛友,然后使用ctm變幻來實現(xiàn)旋轉(zhuǎ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;

}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末薄扁,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子废累,更是在濱河造成了極大的恐慌邓梅,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件邑滨,死亡現(xiàn)場離奇詭異日缨,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)掖看,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進(jìn)店門匣距,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人哎壳,你說我怎么就攤上這事毅待。” “怎么了归榕?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵尸红,是天一觀的道長。 經(jīng)常有香客問我,道長外里,這世上最難降的妖魔是什么怎爵? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮盅蝗,結(jié)果婚禮上鳖链,老公的妹妹穿的比我還像新娘。我一直安慰自己墩莫,他們只是感情好芙委,可當(dāng)我...
    茶點故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著狂秦,像睡著了一般题山。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上故痊,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天,我揣著相機(jī)與錄音玖姑,去河邊找鬼愕秫。 笑死,一個胖子當(dāng)著我的面吹牛焰络,可吹牛的內(nèi)容都是我干的戴甩。 我是一名探鬼主播,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼闪彼,長吁一口氣:“原來是場噩夢啊……” “哼甜孤!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起畏腕,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤缴川,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后描馅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體把夸,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年铭污,在試婚紗的時候發(fā)現(xiàn)自己被綠了恋日。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡嘹狞,死狀恐怖岂膳,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情磅网,我是刑警寧澤谈截,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響傻盟,放射性物質(zhì)發(fā)生泄漏速蕊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一娘赴、第九天 我趴在偏房一處隱蔽的房頂上張望规哲。 院中可真熱鬧,春花似錦诽表、人聲如沸唉锌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽袄简。三九已至,卻和暖如春泛啸,著一層夾襖步出監(jiān)牢的瞬間绿语,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工候址, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留吕粹,地道東北人。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓岗仑,卻偏偏與公主長得像匹耕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子荠雕,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,922評論 2 361