UIImage方向詳解

前言

曾經做過一個圖片編輯軟件年碘,對圖片進行添加貼紙 濾鏡等功能橘茉,其中遇到一個問題工腋,因為對圖片質量要求較高,需要對原圖進行編輯處理畅卓,而不能使用截屏夷蚊,對原圖處理就涉及到圖片方向的概念。在這里對UIImage的方向做一個總結髓介。

圖片格式

主流圖片有兩種格式PNG以及JPG(JPEG)

PNG

因為Apple會對PNG圖片進行優(yōu)化(通過pngcrush)惕鼓,所以資源文件使用PNG格式進行存儲。PNG圖片無方向性唐础,通過MetaData可以看到箱歧。

更多信息.png

JPG

JPG圖片是有損壓縮的,主要用于網絡傳輸一膨。iOS設備相冊存儲的圖片也是此種格式呀邢。
JPG圖片是有方向的,

更多信息_和_CameraDemo.png

JPG方向介紹

orient_flag2.gif

由上圖可知豹绪,JPG圖片的方向是指展示出來的圖像相對于的原圖的變換价淌。舉個例子:
方向6,注釋是逆時針旋轉90度瞒津,也就是目前看到的圖像是原圖 逆時針旋轉90度之后的圖形蝉衣。原圖的意思就是存儲在硬盤上

iOS設備方向

在研究圖片方向前,先要了解設備方向

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
}

具體對應就是

1353491265_4061.png
方向 方向值 描述
UIDeviceOrientationPortrait 6 逆時針旋轉90度
UIDeviceOrientationPortraitUpsideDown 8 順時針旋轉90度
UIDeviceOrientationLandscapeLeft 1 方向正常
UIDeviceOrientationLandscapeRight 3 旋轉180度

以下是使用

方向 方向值 描述
UIDeviceOrientationPortrait 6 逆時針旋轉90度
UIDeviceOrientationPortraitUpsideDown 8 順時針旋轉90度
UIDeviceOrientationLandscapeLeft 1 方向正常
UIDeviceOrientationLandscapeRight 3 旋轉180度

除了這些方向還有其他方向巷蚪,當然這些方向是flip不是直接拍出來的病毡。

orient_flag.gif

具體表如下:

EXIF Orientation Value Row 0 is Column 0 is
1 Top Left side
2 * Top Right side
3 Bottom Right side
4 * Bottom Left side
5 * Left side Top
6 Right side Top
7 * Right side Bottom
8 Left side Bottom

NOTE: Values with "*" are uncommon since they represent "flipped" orientations.

UIImage方向

UIImage官方說明

UIImageOrientationUp

The default orientation of images. The image is drawn right-side up, as shown here.
1494991025796.png

UIImageOrientationDown

The image is rotated 180 degrees, as shown here.
1494991581847.png

UIImageOrientationLeft

The image is rotated 90 degrees clockwise, as shown here.
1494991633798.png

UIImageOrientationRight

The image is rotated 90 degrees counterclockwise, as shown here.
1494991869533.png

UIImageOrientationUpMirrored

The image is drawn as a mirror version of an image drawn with the UIImageOrientationUp value. In other words, the image is flipped along its horizontal axis, as shown here.
1494991906315.png

UIImageOrientationDownMirrored

The image is drawn as a mirror version of an image drawn with the UIImageOrientationDown value. This is the equivalent to flipping an image in the “up” orientation along its horizontal axis and then rotating the image 180 degrees, as shown here.
1494991953891.png

UIImageOrientationLeftMirrored

The image is drawn as a mirror version of an image drawn with the UIImageOrientationLeft value. This is the equivalent to flipping an image in the “up” orientation along its horizontal axis and then rotating the image 90 degrees counterclockwise, as shown here.
1494991990057.png

UIImageOrientationRightMirrored

The image is drawn as a mirror version of an image drawn with the UIImageOrientationRight value. This is the equivalent to flipping an image in the “up” orientation along its horizontal axis and then rotating the image 90 degrees clockwise, as shown here.
1494992030570.png

舉例

UIImage的方向跟JPG圖片方向是一致的,也就是說UIImageOrientationUp是圖片的原始方向屁柏,使用UIImageView看到的圖像是經過原始圖像轉換之后的結果啦膜。


例如:一張圖片的方向是UIImageOrientationLeft有送,使用UIImageView展示,就是使用原始圖像經過順時針旋轉90度之后的結果僧家。

使用UIDeviceOrientationPortrait拍照一張雀摘,預覽圖

ScreenShot_20170518150741.png

而后打斷點

ViewController_m.png

查看原圖

image.tiff.png

圖片方向UIImageOrientationLeft,很明顯預覽圖原圖經過順時針旋轉90度而來。


總結

  • UIImageView如果發(fā)現(xiàn)image的imageOrientation不為UIImageOrientationUp時八拱,imageView會根據imageOrientation調整顯示內容届宠。
    因此,我們把如果image的imageOrientation調整為UIImageOrientationUp就可以看到image的原始圖。
  • 也可以通過內存地址查看原始圖。
  • UIImage是CGImage的上層表現(xiàn)蝠嘉,CGImage真正存儲了UIImage的數(shù)據跳座。
  • 圖片的寬高是按照展示的圖片算的,不是原圖的寬高每聪。
  • 為了精確展示圖片旋轉的過程使用UIViewContentModeScaleAspectFit參數(shù)來展示圖片旦棉。

Demo說明

拍照查看圖片方向

ScreenShot_20170518140710.png

查看預覽圖 原圖以及修正圖和 旋轉過程

ScreenShot_20170518140723.png

Demo地址

git地址TSImageOrientation

參考1

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市药薯,隨后出現(xiàn)的幾起案子绑洛,更是在濱河造成了極大的恐慌,老刑警劉巖童本,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件真屯,死亡現(xiàn)場離奇詭異,居然都是意外死亡穷娱,警方通過查閱死者的電腦和手機绑蔫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來泵额,“玉大人配深,你說我怎么就攤上這事〖廾ぃ” “怎么了篓叶?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長羞秤。 經常有香客問我缸托,道長,這世上最難降的妖魔是什么瘾蛋? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任嗦董,我火速辦了婚禮,結果婚禮上瘦黑,老公的妹妹穿的比我還像新娘京革。我一直安慰自己奇唤,他們只是感情好,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布匹摇。 她就那樣靜靜地躺著咬扇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪廊勃。 梳的紋絲不亂的頭發(fā)上懈贺,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天,我揣著相機與錄音坡垫,去河邊找鬼梭灿。 笑死,一個胖子當著我的面吹牛冰悠,可吹牛的內容都是我干的堡妒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼溉卓,長吁一口氣:“原來是場噩夢啊……” “哼皮迟!你這毒婦竟也來了?” 一聲冷哼從身側響起桑寨,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤伏尼,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后尉尾,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體爆阶,經...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年沙咏,在試婚紗的時候發(fā)現(xiàn)自己被綠了扰她。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡芭碍,死狀恐怖徒役,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情窖壕,我是刑警寧澤忧勿,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站瞻讽,受9級特大地震影響鸳吸,放射性物質發(fā)生泄漏。R本人自食惡果不足惜速勇,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一晌砾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧烦磁,春花似錦养匈、人聲如沸哼勇。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽积担。三九已至,卻和暖如春猬仁,著一層夾襖步出監(jiān)牢的瞬間帝璧,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工湿刽, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留的烁,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓诈闺,卻偏偏與公主長得像渴庆,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子买雾,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,505評論 0 23
  • 晚上約了新朋友出來跑步,我們嫌路上人多嘈雜杨帽,她提議去了路旁的小樹林漓穿。小樹林里樹木茂密,一棵棵樹筆直指向云霄注盈,樹下是...
    燦若星辰Amy閱讀 153評論 0 0
  • 很久沒有寫過東西老客。今天毫無目的的僚饭,只是記錄一些心情。然后希望我的好姑娘早點遇見她的蓋世英雄胧砰,有一天鳍鸵,會身披金甲圣衣...
    Ella劉一嬌閱讀 245評論 0 1
  • 1. “叮叮……”剛剛打開微博界面尉间,看到很久沒有聯(lián)系的朋友發(fā)來的消息偿乖。高中畢業(yè)后,各自去了不同的學校哲嘲,不同的城市贪薪。...
    井鳶閱讀 428評論 3 1
  • 本文原創(chuàng),轉載請注明出處眠副。歡迎關注我的 簡書 画切,關注我的專題 Android Class 我會長期堅持為大家收錄簡...
    MeloDev閱讀 1,637評論 6 19