图片的方向共有8种,图片的方向被包含在Exif中,而Exif则属于图片的一部分。EXIF(Exchangeable Image File)是“可交换图像文件”的缩写,当中包含了专门为数码相机的照片而定制的元数据,可以记录数码照片的拍摄参数、缩略图及其他属性信息。
以R为例,看下iOS中图片的方向








PNG图片不包含方向信息
在UIImage.h文件中,Apple关于UIImageOrientation的注释是有问题的,具体是UIImageOrientationLeft
和UIImageOrientationRight
的注释反过来了。应该是
UIImageOrientationLeft
//The image is rotated 90 degrees clockwise
UIImageOrientationRight
//The image is rotated 90 degrees counterclockwise
在Apple的UIImage Class Reference中,关于Orention的注释是正确的的,
在SDWebImage中,也对图片的方向进行了处理,根据exif中存储的方向信息,将图片的方向转换为iOS中的方向。
+ (UIImageOrientation) sd_exifOrientationToiOSOrientation:(int)exifOrientation
{
UIImageOrientation orientation = UIImageOrientationUp;
switch (exifOrientation)
{
case 1:
orientation = UIImageOrientationUp;
break;
case 3:
orientation = UIImageOrientationDown;
break;
case 8:
orientation = UIImageOrientationLeft;
break;
case 6:
orientation = UIImageOrientationRight;
break;
case 2:
orientation = UIImageOrientationUpMirrored;
break;
case 4:
orientation = UIImageOrientationDownMirrored;
break;
case 5:
orientation = UIImageOrientationLeftMirrored;
break;
case 7:
orientation = UIImageOrientationRightMirrored;
break;
default:
break;
}
return orientation;
}
关键点在于如何从Exif存储的方向转换到UIKit中的方向
这里以字形F为例来进行说明。方向1为正方向。其他的方向通过旋转、沿X轴、Y轴变换都可以变成方向1。
先对表格的含义解释下,表格中0th Row即首行,0th Column即首列,top、bottom、left side、right side表示上下左右。综合起来的意思就是其他任何方向转换到方向1后,转换前的首行和首列在转换后的位置。转换前任何方向的0th Row和0th Column都是top和left side。以方向2为例,沿Y轴翻转后转换为方向1,0th Row不变,0th Column变为right side。方向6顺时针旋转90°后变为方向1后,0th Row变为right side,0th Column变为top。
参考:
1.如何处理iOS中照片的方向
2.Exif Orientation Tag