很多小伙伴瀏覽圖片網(wǎng)站的時(shí)候,會(huì)發(fā)現(xiàn)圖片的一些拍攝參數(shù),包括光圈、像素、鏡頭、相機(jī)等,這些信息就是EXIF信息。
什么是EXIF
EXIF(Exchangeable Image File)是“可交換圖像文件”的縮寫,當(dāng)中包含了專門為數(shù)碼相機(jī)的照片而定制的元數(shù)據(jù),可以記錄數(shù)碼照片的拍攝參數(shù)、縮略圖及其他屬性信息。Exif標(biāo)準(zhǔn)最初由日本電子工業(yè)發(fā)展協(xié)會(huì)(JEIDA - Japan Electronic Industry Development Association)制訂,目前的最新版本是發(fā)布于2010年04月的 Exif 2.3 版,該版本曾在2012年12月以及2013年5月有所修正,并已經(jīng)應(yīng)用到各個(gè)廠商的新影像設(shè)備中。
簡(jiǎn)單來(lái)說(shuō),Exif 信息就是由數(shù)碼相機(jī)在拍攝過(guò)程中采集一系列的信息,然后把信息放置在我們熟知的 JPEG/TIFF 文件的頭部,也就是說(shuō) Exif信息是鑲嵌在 JPEG/TIFF 圖像文件格式內(nèi)的一組拍攝參數(shù),它就好像是傻瓜相機(jī)的日期打印功能一樣,只不過(guò) Exif信息所記錄的資訊更為詳盡和完備。Exif 所記錄的元數(shù)據(jù)信息非常豐富,主要包含了以下幾類信息:
- 拍攝日期
- 拍攝器材(機(jī)身、鏡頭、閃光燈等)
- 拍攝參數(shù)(快門速度、光圈F值、ISO速度、焦距、測(cè)光模式等)
- 圖像處理參數(shù)(銳化、對(duì)比度、飽和度、白平衡等)
- 圖像描述及版權(quán)信息
- GPS定位數(shù)據(jù)
- 縮略圖
EXIF標(biāo)簽
iOS中如何取得圖片的EXIF信息
要取得EXIF信息,首先要獲取到圖片的原始數(shù)據(jù)。iOS是通過(guò)UIImagePickerController這個(gè)類來(lái)獲取圖片的,下面我們就通過(guò)代碼來(lái)說(shuō)明。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* tmpHeaderImg = [info objectForKey:UIImagePickerControllerEditedImage];
NSURL *fileUrl = [info objectForKey:UIImagePickerControllerImageURL];
//有的圖片選擇器返回的key是PHImageFileURLKey
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
CFDictionaryRef imageInfo = CGImageSourceCopyPropertiesAtIndex(imageSource, 0,NULL);
//imageInfo 為圖片全部EXIF信息
[self.image setImage:tmpHeaderImg];
}
代碼寫到這里用模擬器測(cè)試會(huì)發(fā)現(xiàn),能獲得圖片的EXIF信息,但是一用真機(jī)運(yùn)行就崩潰,這是為什么呢?
大家都知道,iOS上的APP只能取到沙盒里面的數(shù)據(jù),而PHImageFileURLKey給出的地址是圖片在手機(jī)里面的存儲(chǔ)地址,APP是沒(méi)有權(quán)限獲取的。這時(shí)候我們就要用Asset來(lái)獲取原始圖片數(shù)據(jù)
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
NSDictionary *imageInfo = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
}];
通過(guò)Asset獲取圖片適用于真機(jī)和模擬器,這樣想服務(wù)器上傳上述方法返回的imageData,就可以獲得含有EXIF信息的圖片了。如果不獲取圖片原始數(shù)據(jù)直接從圖片選擇器上傳圖片,蘋果或基于安全和隱私方面的考慮,會(huì)隱藏大部分的EXIF信息。
以下是上方圖片的EXIF信息
Printing description of imageInfo:
{
ColorModel = RGB;
DPIHeight = 72;
DPIWidth = 72;
Depth = 8;
Orientation = 1;
PixelHeight = 3456;
PixelWidth = 5184;
ProfileName = "sRGB IEC61966-2.1";
"{ExifAux}" = {
AFInfo = (
"0.4895833",
"0.7025463",
"0.02083333",
"0.02488426",
n,
"0.3256173",
"0.598235",
"0.01697531",
"0.03096065",
n,
"0.6574074",
"0.598235",
"0.01697531",
"0.03096065",
n,
"0.2276234",
"0.4845197",
"0.01697531",
"0.03096065",
n,
"0.4875579",
"0.4810475",
"0.02488426",
"0.03790509",
F,
"0.7554013",
"0.4845197",
"0.01697531",
"0.03096065",
n,
"0.3256173",
"0.3708044",
"0.01697531",
"0.03096065",
n,
"0.6574074",
"0.3708044",
"0.01697531",
"0.03096065",
n,
"0.4895833",
"0.2725694",
"0.02083333",
"0.02488426",
n
);
Firmware = "Firmware Version 1.1.1";
FlashCompensation = 0;
FocusMode = 1;
ImageStabilization = 3;
LensID = 51;
LensInfo = (
18,
135,
0,
0,
18,
135,
0,
0
);
LensModel = "Canon EF-S 18-135mm f/3.5-5.6 IS";
SerialNumber = 331505805;
};
"{Exif}" = {
ApertureValue = 5;
BodySerialNumber = 0331505805;
ColorSpace = 1;
ComponentsConfiguration = (
1,
2,
3,
0
);
CustomRendered = 0;
DateTimeDigitized = "2017:04:04 10:09:52";
DateTimeOriginal = "2017:04:04 10:09:52";
ExifVersion = (
2,
3
);
ExposureBiasValue = 0;
ExposureMode = 0;
ExposureProgram = 3;
ExposureTime = "0.01";
FNumber = "5.6";
Flash = 16;
FlashPixVersion = (
1,
0
);
FocalLength = 135;
FocalPlaneResolutionUnit = 2;
FocalPlaneXResolution = "5728.176470588235";
FocalPlaneYResolution = "5808.403508771929";
ISOSpeedRatings = (
100
);
LensModel = "EF-S18-135mm f/3.5-5.6 IS";
LensSerialNumber = 000012e3b8;
LensSpecification = (
18,
135,
0,
0
);
MaxApertureValue = "5.656853725850966";
MeteringMode = 5;
PixelXDimension = 5184;
PixelYDimension = 3456;
RecommendedExposureIndex = 100;
SceneCaptureType = 0;
SensitivityType = 2;
ShutterSpeedValue = "6.625";
SubsecTime = 00;
SubsecTimeDigitized = 00;
SubsecTimeOriginal = 00;
WhiteBalance = 0;
};
"{IPTC}" = {
Byline = (
""
);
CopyrightNotice = "";
StarRating = 0;
};
"{MakerCanon}" = {
CameraTemperatur = 15;
ContinuousDrive = 0;
FocusDistanceLower = "1.66";
FocusDistanceUpper = "1.82";
LensModel = "EF-S18-135mm f/3.5-5.6 IS";
MaxAperture = "5.656854249492381";
MinAperture = "35.91878554589994";
UniqueModelID = 2147484295;
WhiteBalanceIndex = 0;
};
"{PictureStyle}" = {
ColorTone = (
0,
0,
0
);
Monochrome = (
0,
0,
0
);
PictStyleColorSpace = (
sRGB,
1,
1
);
PictureStyle = (
4095,
4095,
4095
);
SharpnessFreq = (
0,
0,
0
);
};
"{TIFF}" = {
Artist = "";
Copyright = "";
DateTime = "2017:04:04 10:09:52";
Make = Canon;
Model = "Canon EOS 60D";
Orientation = 1;
ResolutionUnit = 2;
XResolution = 72;
YResolution = 72;
};
}
如何修改圖片的EXIF
- 獲取圖片中的EXIF文件
NSMutableDictionary *metaDataDic = [imageInfo mutableCopy];
NSMutableDictionary *exifDic =[[metaDataDic objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
- 修改EXIF文件中的部分信息
[exifDic setObject:[NSNumber numberWithFloat:123123] forKey:(NSString *)kCGImagePropertyExifExposureTime];
[exifDic setObject:@"SenseTime" forKey:(NSString *)kCGImagePropertyExifLensModel];
[metaDataDic setObject:exifDic forKey:(NSString*)kCGImagePropertyExifDictionary];
- 將修改后的文件寫入至圖片中
CFStringRef UTI = CGImageSourceGetType(imageSource);
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);
- 保存圖片
NSString *directoryDocuments = NSTemporaryDirectory();
[newImageData writeToFile: directoryDocuments atomically:YES];
- 查看修改后圖片的Exif信息
CIImage *testImage = [CIImage imageWithData:newImageData];
NSDictionary *propDict = [testImage properties];
NSLog(@"Properties %@", propDict);
注意事項(xiàng)
關(guān)于無(wú)法修改Exif值的幾點(diǎn)注意事項(xiàng):
- 傳入的數(shù)據(jù)格式與Exif規(guī)定的不符
Exif的每條信息都有對(duì)應(yīng)的數(shù)據(jù)類型,如:String Float... 如果數(shù)據(jù)類型傳入錯(cuò)誤將無(wú)法寫入文件。- 傳入的字段超過(guò)規(guī)定字段長(zhǎng)度
- 相互依賴的字段只添加了一個(gè)字段
在GPS文件中經(jīng)緯度的度數(shù)的字段與經(jīng)緯度的方向的字段相互依賴,修改經(jīng)/緯度數(shù)需要經(jīng)/緯方向字段的存在,否則修改無(wú)效。