1.Exif簡介
可交換圖像文件格式常被簡稱為Exif(Exchangeable image file format)哩掺,是專門為數(shù)碼相機(jī)的照片設(shè)定的,可以記錄數(shù)碼照片的屬性信息和拍攝數(shù)據(jù)涩笤。
Exif可以附加于JPEG嚼吞、TIFF、RIFF蹬碧、EXIF舱禽、GPS等文件之中,為其增加有關(guān)數(shù)碼相機(jī)拍攝信息的內(nèi)容和索引圖或圖像處理軟件的版本信息恩沽。
Exif信息以0xFFE1作為開頭標(biāo)記誊稚,后兩個(gè)字節(jié)表示Exif信息的長度。所以Exif信息最大為64 kB罗心,而內(nèi)部采用TIFF格式里伯。
2.讀取Exif信息
Exif信息是通過ImageIO框架來實(shí)現(xiàn)的,ImageIO框架在iOS中偏低層渤闷,所以提供的接口都是C風(fēng)格的疾瓮,關(guān)鍵數(shù)據(jù)也都是使用CoreFoundation進(jìn)行存儲(chǔ)。進(jìn)行數(shù)據(jù)的操作也就需要CoreFoundation和上層Foundation之間進(jìn)行橋接轉(zhuǎn)換肤晓。
1. 獲取圖片文件
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"YourPic" withExtension:@""];
2.創(chuàng)建CGImageSourceRef
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
3.利用imageSource獲取全部ExifData
CFDictionaryRef imageInfo = CGImageSourceCopyPropertiesAtIndex(imageSource, 0,NULL);
4.從全部ExifData中取出EXIF文件
NSDictionary *exifDic = (__bridge NSDictionary *)CFDictionaryGetValue(imageInfo, kCGImagePropertyExifDictionary) ;
5.打印全部Exif信息及EXIF文件信息
NSLog(@"All Exif Info:%@",imageInfo);
NSLog(@"EXIF:%@",exifDic);
一張佳能相機(jī)拍攝的照片中的Exif信息:
All Exif Info:{
ColorModel = RGB;
DPIHeight = 200;
DPIWidth = 200;
Depth = 8;
Orientation = 1;
PixelHeight = 667;
PixelWidth = 1000;
ProfileName = "sRGB IEC61966-2.1";
"{Exif}" = {
ApertureValue = "3.375";
BodySerialNumber = 214014001512;
ColorSpace = 1;
ComponentsConfiguration = (
1,
2,
3,
0
);
CustomRendered = 0;
DateTimeDigitized = "2016:07:05 16:12:02";
DateTimeOriginal = "2016:07:05 16:12:02";
ExifVersion = (
2,
3
);
ExposureBiasValue = 0;
ExposureMode = 0;
ExposureProgram = 3;
ExposureTime = "0.0004";
FNumber = "3.2";
Flash = 16;
FlashPixVersion = (
1,
0
);
FocalLength = 168;
FocalPlaneResolutionUnit = 2;
FocalPlaneXResolution = "3545.827586206897";
FocalPlaneYResolution = "3526.530612244898";
ISOSpeedRatings = (
400
);
LensMake = "Canon 35mm f1.4";
LensModel = "2016/09/21 11:04:31";
LensSerialNumber = 0000c08f5f;
LensSpecification = (
70,
200,
0,
0
);
MeteringMode = 3;
PixelXDimension = 1000;
PixelYDimension = 667;
RecommendedExposureIndex = 400;
SceneCaptureType = 0;
SensitivityType = 2;
ShutterSpeedValue = "11.375";
SubsecTime = 795;
SubsecTimeDigitized = 30;
SubsecTimeOriginal = 30;
WhiteBalance = 0;
};
"{IPTC}" = {
DateCreated = 20160705;
DigitalCreationDate = 20160705;
DigitalCreationTime = 161202;
TimeCreated = 161202;
};
"{JFIF}" = {
DensityUnit = 0;
JFIFVersion = (
1,
0,
1
);
XDensity = 200;
YDensity = 200;
};
"{TIFF}" = {
DateTime = "2016:07:08 16:45:32";
Make = Canon;
Model = "Canon EOS-1D X";
Orientation = 1;
ResolutionUnit = 2;
Software = "ACDSee Pro 8";
XResolution = 200;
YResolution = 200;
};
}
3.寫入Exif信息
1. 獲取圖片中的EXIF文件和GPS文件
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"YourImage"], 1);
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
NSDictionary *imageInfo = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
NSMutableDictionary *metaDataDic = [imageInfo mutableCopy];
NSMutableDictionary *exifDic =[[metaDataDic objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
NSMutableDictionary *GPSDic =[[metaDataDic objectForKey:(NSString*)kCGImagePropertyGPSDictionary]mutableCopy];
2. 修改EXIF文件和GPS文件中的部分信息
[exifDic setObject:[NSNumber numberWithFloat:1234.3] forKey:(NSString *)kCGImagePropertyExifExposureTime];
[exifDic setObject:@"SenseTime" forKey:(NSString *)kCGImagePropertyExifLensModel];
[GPSDic setObject:@"N" forKey:(NSString*)kCGImagePropertyGPSLatitudeRef];
[GPSDic setObject:[NSNumber numberWithFloat:116.29353] forKey:(NSString*)kCGImagePropertyGPSLatitude];
[metaDataDic setObject:exifDic forKey:(NSString*)kCGImagePropertyExifDictionary];
[metaDataDic setObject:GPSDic forKey:(NSString*)kCGImagePropertyGPSDictionary];
3. 將修改后的文件寫入至圖片中
CFStringRef UTI = CGImageSourceGetType(source);
NSMutableData *newImageData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)newImageData, UTI, 1,NULL);
//add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metaDataDic);
CGImageDestinationFinalize(destination)爷贫;
4. 保存圖片
NSString *directoryDocuments = NSTemporaryDirectory();
[newImageData writeToFile: directoryDocuments atomically:YES];
5. 查看修改后圖片的Exif信息
CIImage *testImage = [CIImage imageWithData:newImageData];
NSDictionary *propDict = [testImage properties];
NSLog(@"Properties %@", propDict);
修改后的Exif信息:
Properties {
ColorModel = RGB;
Depth = 8;
Orientation = 1;
PixelHeight = 667;
PixelWidth = 1000;
ProfileName = "sRGB IEC61966-2.1";
"{Exif}" = {
ColorSpace = 1;
ExposureTime = "1234.3";
LensModel = SenseTime;
PixelXDimension = 1000;
PixelYDimension = 667;
};
"{GPS}" = {
Latitude = "116.2935333333333";
LatitudeRef = N;
};
"{JFIF}" = {
DensityUnit = 0;
JFIFVersion = (
1,
0,
1
);
XDensity = 72;
YDensity = 72;
};
"{TIFF}" = {
Orientation = 1;
};
}
4.注意事項(xiàng)
關(guān)于無法修改Exif值的幾點(diǎn)注意事項(xiàng):
1. 傳入的數(shù)據(jù)格式與Exif規(guī)定的不符
Exif的每條信息都有對(duì)應(yīng)的數(shù)據(jù)類型认然,如:String Float... 如果數(shù)據(jù)類型傳入錯(cuò)誤將無法寫入文件。
2. 傳入的字段超過規(guī)定字段長度
3. 相互依賴的字段只添加了一個(gè)字段
在GPS文件中經(jīng)緯度的度數(shù)的字段與經(jīng)緯度的方向的字段相互依賴漫萄,修改經(jīng)/緯度數(shù)需要經(jīng)/緯方向字段的存在卷员,否則修改無效。