和图片有关的几个旋转属性:
1 UIImage的UIImageOrientation属性。常见并且也是网上很容易查到的一个属性。值从0开始:
typedef NS_ENUM(NSInteger,UIImageOrientation) {
UIImageOrientationUp, // default orientation
UIImageOrientationDown, // 180 deg rotation
UIImageOrientationLeft, // 90 deg CCW
UIImageOrientationRight, // 90 deg CW
UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip
UIImageOrientationDownMirrored, // horizontal flip
UIImageOrientationLeftMirrored, // vertical flip
UIImageOrientationRightMirrored, // vertical flip
};
(注:根据标准C枚举默认从0开始)
2 CGImageSourceRef生成的CFDictionaryRef可以查询到kCGImagePropertyOrientation属性:
1 = 0th row is at the top, and 0thcolumn is on the left.
2 = 0th row is at the top, and 0thcolumn is on the right.
3 = 0th row is at the bottom, and0th column is on the right.
4 = 0th row is at the bottom, and0th column is on the left.
5 = 0th row is on the left, and 0thcolumn is the top.
6 = 0th row is on the right, and0th column is the top.
7 = 0th row is on the right, and0th column is the bottom.
8 = 0th row is on the left, and 0thcolumn is the bottom.
根据文档中的注释我们可以知道这个值是和TIFF和Exif一致的(后门会提到TIFF和EXIF)。值是从1开始(妈蛋不像枚举默认一样是从0开始!!,具体每个值的含义查翻译自!己!脑!补!)。(上面的解释其实就是在描述视觉上的第0行第0列在图片中的上下左右的位置,写个“上”字有助于想象。)
3 EXIF:实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍摄时的光圈、快门、白平衡、ISO、焦距、日期时间等各种和拍摄条件以及相机品牌、型号、色彩编码、拍摄时录制的声音以及全球定位系统(GPS)、缩略图等。简单地说,Exif=JPEG+拍摄参数。因此,你可以利用任何可以查看JPEG文件的看图软件浏览Exif格式的照片,但并不是所有的图形程序都能处理Exif信息。(来自百度百科,需要注意的是百度百科和在程序中输出的EXIF属性并不一致,比如包含的内容、属性的名称。)
4 TIFF:TIFF是Tag ImageFile Format的缩写,这不仅仅是一种文件扩展名或格式,而是一种开放标准,核心在于动态的标记存储能力,就是Key/Value对的存储能力。
该标准很多老牌大公司如Adobe都参与过修订,目前的元数据(MetaData)是经过很多年演变后几近统一的,TIFF除了支持动态的标记规范,还支持例如单文件多图数据存储(Pages)、缩略图等等的特性。
TIFF的应用范围大致上有GIF, TIFF,JPG/JPG2000等,无PNG。
而EXIF是主要由多家相机厂商推行的标准,用于标识出拍摄时的摄影信息,是另外一个厂商级的规范,该规范建立在TIFF格式的基础之上,也就是无TIFF就无EXIF,它也是利用了TIFF的组织方式对特定数据进行组织,所以对于PNG这种连TIFF都不遵守的文件格式自然也无所谓EXIF。
另外,由此可见,TIFF是基础,标识了图像的几种重要信息,例如长宽,采样数,分辨率,旋转方向等,而EXIF则会对光圈,快门等摄影信息进行丰富,两者或许会有点点重叠,但是TIFF有的属性肯定是以TIFF为准,并且有的图像有TIFF却不一定会有EXIF。可参考 http://www.awaresystems.be/imaging/tiff/faq.html相关深入内容大家自行挖掘)
获取iPhone相片的信息中也能证实以上的内容,根据(代码自己放大了看,包括了获取方向的代码)
NSData *adata = [[NSDataalloc] initWithBytesNoCopy:buffer length:image_representation.sizefreeWhenDone:YES];
NSDictionary*sourceOptionsDict = [NSDictionarydictionaryWithObjectsAndKeys:(id)[image_representation UTI] ,kCGImageSourceTypeIdentifierHint,nil];
CGImageSourceRefsourceRef = CGImageSourceCreateWithData((__bridge CFDataRef) adata, (__bridge CFDictionaryRef)sourceOptionsDict);
CFDictionaryRefimagePropertiesDictionary;
imagePropertiesDictionary =CGImageSourceCopyPropertiesAtIndex(sourceRef,0, NULL);
CFDictionaryReftiff = (CFDictionaryRef)CFDictionaryGetValue(imagePropertiesDictionary,kCGImagePropertyTIFFDictionary);
NSDictionary*tiff_dict = (__bridge NSDictionary*)tiff;
NSIntegerorientation = [[tiff_dict objectForKey:@"Orientation"] integerValue];
以上代码可以获得相片的TIFF信息,类比可以获得EXIF信息。在控制台输出字典imagePropertiesDictionary信息可以看到key TIFF和EXIF。百度百科上列举的一些EXIF属性其实就包含在了TIFF对应的value中。
研发过程中发生的一个问题:大图显示正常但是服务器获取的缩略图可能颠倒的问题,,总结了一下问题的原因。在用uiimage进行图片颠倒的时候并写文件的时候,很有可能没有操作原图而是在操作生成的快照,这样上传给服务器的图片中TIIF中旋转的信息并没有更改,而服务器 生成原始图片的时候丢弃了这些信息就会生成一个看起来很正常的图片,但是生成缩略图的时候服务器组件貌似可能大概处理过后保留了这个旋转信息,这就导致了服务器同一张图片的大图正常但是缩略图旋转的问题。
以上内容纯属抄袭,如有问题别来找我。