添加经纬度信息和自定义字段
#import <ImageIO/ImageIO.h>
- (NSData *)addExifDataToImage:(NSData *)imageData withLatitude:(double)latitude longitude:(double)longitude customField:(NSString *)customFieldValue {
// 创建图片数据源
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
// 获取图片属性字典
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
// 创建新的 EXIF 信息字典
NSMutableDictionary *exifDictionary = [NSMutableDictionary dictionaryWithDictionary:(__bridge NSDictionary *)CFDictionaryGetValue(properties, kCGImagePropertyExifDictionary)];
// 设置经纬度信息
[exifDictionary setObject:[NSNumber numberWithDouble:latitude] forKey:(NSString *)kCGImagePropertyGPSLatitude];
[exifDictionary setObject:[NSNumber numberWithDouble:longitude] forKey:(NSString *)kCGImagePropertyGPSLongitude];
// 添加自定义字段
[exifDictionary setObject:customFieldValue forKey:@"MyCustomField"];
// 将新的 EXIF 信息字典添加到图片属性字典中
CFMutableDictionaryRef mutableProperties = CFDictionaryCreateMutableCopy(NULL, 0, properties);
CFDictionarySetValue(mutableProperties, kCGImagePropertyExifDictionary, (__bridge CFDictionaryRef)exifDictionary);
// 创建图片数据目标
CGImageDestinationRef destination = CGImageDestinationCreateWithData((CFMutableDataRef)[NSMutableData data], (CFStringRef)CGImageSourceGetType(source), 1, NULL);
// 将图片数据添加到目标对象中,并传入修改后的属性字典
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)mutableProperties);
// 完成写入操作
BOOL success = CGImageDestinationFinalize(destination);
// 获取修改后的图片数据
NSData *updatedImageData = nil;
if (success) {
updatedImageData = (__bridge_transfer NSData *)CGImageDestinationCopyData(destination, 0);
}
// 释放资源
CFRelease(source);
CFRelease(properties);
CFRelease(mutableProperties);
CFRelease(destination);
return updatedImageData;
}
读取exif信息
NSURL *imageFileURL = [NSURL fileURLWithPath:@"/Users/USERNAME/Documents/tasting_menu_004.jpg"];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
NSDictionary *treeDict;
NSMutableString *exifData;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,
nil];
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, ( CFDictionaryRef)options);
CFRelease(imageSource);
if (imageProperties) {
treeDict = [NSDictionary dictionaryWithDictionary:(NSDictionary*)(imageProperties)];
id exifTree = [treeDict objectForKey:(NSString *)kCGImagePropertyExifDictionary];
exifData = [NSMutableString stringWithString:@""];
for (NSString *key in [[exifTree allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
NSString* locKey = [[NSBundle bundleWithIdentifier:@"com.apple.ImageIO.framework"] localizedStringForKey:key value:key table: @"CGImageSource"];
id value = [exifTree valueForKey:key] ;
[exifData appendFormat:@"key =%@ ; Value = %@ \n", locKey,value];
}
NSLog(@" exifData %@", exifData);
}
标签:CFDictionaryRef,读取,exif,NSDictionary,source,NSString,添加,key,CFRelease
From: https://www.cnblogs.com/wgb1234/p/18393623