iOS--app自定義相冊--給圖片重寫exif數據-定義相冊時間戳


1.Exif簡介

可交換圖像文件格式常被簡稱為Exif(Exchangeable image file format),是專門為數碼相機的照片設定的,可以記錄數碼照片的屬性信息和拍攝數據。

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

Exif信息以0xFFE1作為開頭標記,后兩個字節表示Exif信息的長度。所以Exif信息最大為64 kB,而內部采用TIFF格式。

2.讀取Exif信息

Exif信息是通過ImageIO框架來實現的,ImageIO框架在iOS中偏低層,所以提供的接口都是C風格的,關鍵數據也都是使用CoreFoundation進行存儲。進行數據的操作也就需要CoreFoundation和上層Foundation之間進行橋接轉換。

1. 獲取圖片文件

NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"YourPic" withExtension:@""];

2.創建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];

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.注意事項

關於無法修改Exif值的幾點注意事項:

1. 傳入的數據格式與Exif規定的不符

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

2. 傳入的字段超過規定字段長度

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

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

5.外部鏈接


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM