iOS 讀取圖片 exif 信息

版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/wenhaiwang/article/details/77720573
1.Exif簡介
可交換圖像文件格式常被簡稱為Exif(Exchangeable image file format),是專門為數(shù)碼相機的照片設(shè)定的,可以記錄數(shù)碼照片的屬性信息和拍攝數(shù)據(jù)。

Exif可以附加于JPEG、TIFF、RIFF、EXIF、GPS等文件之中,為其增加有關(guān)數(shù)碼相機拍攝信息的內(nèi)容和索引圖或圖像處理軟件的版本信息。

Exif信息以0xFFE1作為開頭標(biāo)記,后兩個字節(jié)表示Exif信息的長度。所以Exif信息最大為64 kB,而內(nèi)部采用TIFF格式。

2.讀取Exif信息
Exif信息是通過ImageIO框架來實現(xiàn)的,ImageIO框架在iOS中偏低層,所以提供的接口都是C風(fēng)格的,關(guān)鍵數(shù)據(jù)也都是使用CoreFoundation進行存儲。進行數(shù)據(jù)的操作也就需要CoreFoundation和上層Foundation之間進行橋接轉(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);
一張佳能相機拍攝的照片中的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];

  1. 修改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];

  1. 將修改后的文件寫入至圖片中

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);

  1. 保存圖片

NSString *directoryDocuments = NSTemporaryDirectory();
[newImageData writeToFile: directoryDocuments atomically:YES];

  1. 查看修改后圖片的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.注意事項
關(guān)于無法修改Exif值的幾點注意事項:

  1. 傳入的數(shù)據(jù)格式與Exif規(guī)定的不符

Exif的每條信息都有對應(yīng)的數(shù)據(jù)類型,如:String Float... 如果數(shù)據(jù)類型傳入錯誤將無法寫入文件。

  1. 傳入的字段超過規(guī)定字段長度

  2. 相互依賴的字段只添加了一個字段

在GPS文件中經(jīng)緯度的度數(shù)的字段與經(jīng)緯度的方向的字段相互依賴,修改經(jīng)/緯度數(shù)需要經(jīng)/緯方向字段的存在,否則修改無效。

5.外部鏈接
Exif2.3官方文檔
Introduction to UTI Overview
Modified EXIF data doesn't save properly
————————————————
版權(quán)聲明:本文為CSDN博主「旺仔餅餅」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/wenhaiwang/article/details/77720573

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容