首页 > 其他分享 >添加和读取图片的exif信息

添加和读取图片的exif信息

时间:2024-09-02 21:52:29浏览次数:3  
标签:CFDictionaryRef 读取 exif NSDictionary source NSString 添加 key CFRelease

添加经纬度信息和自定义字段

#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

相关文章

  • opencv学习:基础用法和图像添加边界框设置
    1.opencv基础用法1.从文件加载图像a=cv2.imread("1.jpg")b=cv2.imread("2.jpg")2.从文件读取视频video=cv2.VideoCapture("1.mp4")3.读摄像头cv2.VideoCapture(0)4.保存图片cv2.imwrite(r"1_gray.jpg",b)5.展示图片cv2.imshow("shuiju",a)6.释......
  • 为代码块添加 Mac OS X 窗口样式
    为代码块添加MacOSX窗口样式为代码块添加MacOSX窗口样式,在代码块pre之前添加图片,在代码块pre之后添加文本。pre{padding:30px2px2px2px;line-height:1;overflow:auto;word-wrap:normal;border-radius:5px;}pre>code{mar......
  • JS的DOM高级编程和动态添加表格行的小案例实现
    DOM高级编程(DocumentobjectModal)DOM概述DOM-DocumentObjectModal,它是W3C国际组织的一套Web标准DOM是一种与浏览器、平台、语言无关的接口Dom认为:html文档中每个成员都是一个节点,根据节点的不同,可分为:文档节点(document)元素节点(element)属性节点(attribute)文本节点(tex......
  • 【vue、react】前端如何为package.json添加注释
    文章目录前言安装使用方法问题前言写了个项目,想给package.json加注释结果发现加不上去,就在网上查找了相关文章,特意总结记录一下,防止下次使用。参考文章:如何为package.json添加注释众所周知,JSON文件是不支持注释的,除了JSON5/JSONC之外,我们在开发项目特别是前端项目......
  • 将新对象添加到特定现有数组名称内,并使用索引值进行保存
    你可以使用JavaScript的数组方法来实现这个功能。以下是一个示例代码:letarray=[{id:1,name:'John'},{id:2,name:'Jane'},{id:3,name:'Jack'}];letnewObject={id:4,name:'Jill'};array[array.length]=newObject;c......
  • Linux系统添加软件快捷方式 (图标)
    Linux系统添加软件快捷方式(图标)背景在Linux系统下安装软件,安装软件后,常常缺少软件的快捷方式,不熟悉Linux系统的同学甚至不知道软件安装的位置,就算知道软件安装位置的同学也会因打开点开一大堆文件夹而头疼,因此,在Linux系统性为常用软件添置快捷方式显得尤为重要。具体方法方法......
  • 添加到MySQL中的json
    要将JSON数据添加到MySQL中,你可以使用MySQL提供的JSON数据类型。以下是一般的步骤:确保你的MySQL版本支持JSON数据类型。JSON数据类型在MySQL5.7及更高版本中可用。创建表时,将包含JSON数据的列定义为JSON类型。例如:CREATETABLEyour_table(idINTPRIMARY......
  • 使用 ...new Set 在 filtred 数组中添加计数重复值
    你可以使用newSet和filter方法来计算数组中重复值的数量。以下是一个示例代码:constarr=[1,2,2,3,3,3,4,4,4,4];constfilteredArr=arr.filter((item,index,self)=>self.indexOf(item)===index);constset=newSet(filteredArr);constcount=fil......
  • 20240908_030339 编程剪辑 读取音频对像获得音频时长
    需求有一个目录里存放了多个音频文件我们要获取这些音频文件的相关信息编写一个方法接收音频文件的目录返回音频信息列表示例......
  • Blazor基础学习填坑系列-02-添加依赖包
    继续学习官方给出的demo,参照示例代码,添加包引用时卡住了,官方给出的包的版本6.0.X明显跟当前用的不一致 心想,就别在终端中敲指令了,干脆手动添加吧,结果找不到入口,摸索了下,终于找到:依赖项-包,右键-管理NuGet程序包 根据依赖包名搜索,安装,版本号会自动带出,代码会自动补全......