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之间进行桥接转换。
- 获取图片文件
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);
复制代码
3.写入Exif信息
- 获取图片中的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];
复制代码
- 修改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];
复制代码
- 将修改后的文件写入至图片中
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);
复制代码
- 保存图片
NSString *directoryDocuments = NSTemporaryDirectory();
[newImageData writeToFile: directoryDocuments atomically:YES];
复制代码
- 查看修改后图片的Exif信息
CIImage *testImage = [CIImage imageWithData:newImageData];
NSDictionary *propDict = [testImage properties];
NSLog(@"Properties %@", propDict);
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END