添加和读取图片的exif信息

添加经纬度信息和自定义字段 可参考 https://github.com/Nikita2k/SimpleExif

#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信息 如果是PHAsset 可参考https://github.com/alexanderkent/PHAsset-Meta

来自 https://github.com/oopww1992/WWimageExif/blob/master/WWimageExifClass/WWimageExitInfo.m

#import <Foundation/Foundation.h>
#import <ImageIO/ImageIO.h>


@interface WWimageExitInfo : NSObject{
    @private
    CGImageSourceRef imageRef;
}

/*初始化方法
 *parms imageUrl 图片路径
 */
- (id)initWithURL:(NSURL*)imageUrl;

//照片EXIF
@property(strong) NSDictionary * imageExifDictionary;
//照片Tiff
@property(strong) NSDictionary * imageTiffDictionary;

//所有照片信息
@property(strong) NSDictionary * imageProperty;

@property(assign,nonatomic) NSInteger  imageFileSize;
@property(strong)           NSString * imageFileType;
@end

#import "WWimageExitInfo.h"

@implementation WWimageExitInfo 
@synthesize imageExifDictionary = _imageExifDictionary;
@synthesize imageTiffDictionary = _imageTiffDictionary;
@synthesize imageProperty,imageFileSize,imageFileType;

- (id)initWithURL:(NSURL*)imageUrl
{
    self = [super init];
    if (self) {
        imageRef = CGImageSourceCreateWithURL((__bridge CFURLRef)imageUrl, NULL);
        imageProperty = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(imageRef, 0, NULL);
        imageFileSize = [self fileSize];
        imageFileType = [self fileType];
        
        self.imageExifDictionary = [self imageExifDictionaryGet];
        self.imageTiffDictionary = [self imageTiffDictionary];
    }
    return self;
}

-(NSDictionary *)imageExifDictionaryGet{
    return [imageProperty valueForKey:(NSString*)kCGImagePropertyExifDictionary];
}
- (NSDictionary*)tiffDictonary{
    return [imageProperty valueForKey:(NSString*)kCGImagePropertyTIFFDictionary];
}
- (NSInteger)fileSize
{
    CFDictionaryRef dict = CGImageSourceCopyProperties(imageRef, NULL);
    CFNumberRef fileSize = (CFNumberRef)CFDictionaryGetValue(dict, kCGImagePropertyFileSize);
    CFNumberType type = CFNumberGetType(fileSize);
    int size = 0;
    if (type == kCFNumberSInt32Type) {
        CFNumberGetValue(fileSize, type, &size);
    }
    CFRelease(dict);
    return 0;
}

- (NSString*)fileType
{
    CFStringRef fileDp = CGImageSourceGetType(imageRef);
    CFStringRef fileType = CFCopyDescription(fileDp);
    NSString *typeRef = (__bridge NSString*)fileType;
    CFRelease(fileType);
    return typeRef;
}


@end

   
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"IMG_0176" withExtension:@"jpg"];
        
    //初始化后  即可使用各项信息
    WWimageExitInfo * imageInfo = [[WWimageExitInfo alloc]initWithURL:fileUrl];
    
    NSLog(@"imageExifMessage --%@",imageInfo.imageExifDictionary);
    NSLog(@"imageTiffMessage --%@",imageInfo.imageTiffDictionary);
    
    //照片所有信息
    NSLog(@"allImageInfo --%@",imageInfo.imageProperty);
    
    NSLog(@"fileSize --%d",imageInfo.imageFileSize);
    NSLog(@"fileType --%@",imageInfo.imageFileType);

或许还可以采用这个方案: 将数据隐藏在图片当中,并可以进行还原,可以用于iOS或者或者MacOS应用
https://github.com/fenglh/PictureSteganography

posted @ 2024-09-02 21:56  CoderWGB  阅读(8)  评论(0编辑  收藏  举报