前言
曾經做過一個圖片編輯軟件年碘,對圖片進行添加貼紙
濾鏡
等功能橘茉,其中遇到一個問題工腋,因為對圖片質量要求較高,需要對原圖
進行編輯處理畅卓,而不能使用截屏夷蚊,對原圖
處理就涉及到圖片方向
的概念。在這里對UIImage
的方向做一個總結髓介。
圖片格式
主流圖片有兩種格式PNG
以及JPG(JPEG)
PNG
因為Apple
會對PNG
圖片進行優(yōu)化(通過pngcrush
)惕鼓,所以資源文件使用PNG
格式進行存儲。PNG
圖片無方向性唐础,通過MetaData
可以看到箱歧。
JPG
JPG
圖片是有損壓縮的,主要用于網絡傳輸一膨。iOS
設備相冊存儲的圖片也是此種格式呀邢。
JPG
圖片是有方向的,
JPG
方向介紹
由上圖可知豹绪,
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
}
具體對應就是
方向 | 方向值 | 描述 |
---|---|---|
UIDeviceOrientationPortrait | 6 | 逆時針旋轉90度 |
UIDeviceOrientationPortraitUpsideDown | 8 | 順時針旋轉90度 |
UIDeviceOrientationLandscapeLeft | 1 | 方向正常 |
UIDeviceOrientationLandscapeRight | 3 | 旋轉180度 |
以下是使用
方向 | 方向值 | 描述 |
---|---|---|
UIDeviceOrientationPortrait | 6 | 逆時針旋轉90度 |
UIDeviceOrientationPortraitUpsideDown | 8 | 順時針旋轉90度 |
UIDeviceOrientationLandscapeLeft | 1 | 方向正常 |
UIDeviceOrientationLandscapeRight | 3 | 旋轉180度 |
除了這些方向還有其他方向巷蚪,當然這些方向是flip
不是直接拍出來的病毡。
具體表如下:
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
拍照一張雀摘,預覽圖
而后打斷點
查看原圖
圖片方向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
說明
拍照查看圖片方向
查看預覽圖
原圖
以及修正圖
和 旋轉過程
Demo
地址
git
地址TSImageOrientation