转载:https://blog.csdn.net/solar1937/article/details/50913148
今天学习了imageIO底层框架,由于公司有一个需求用imagepicker拍出来的照片没有exif信息,需要把照片添加设置拍摄时间等exif信息,所以上网查到一些资料,在这里整理一下希望对大家有所帮助
一、在使用imageIO框架时需要导入头文件
#import
这样可以使用ImageIO里面的
CGImageSourceRef 和 CGImageDestinationRef两个类了,CGImageSourceRef功能是读取图像数据,读取图像的缩略图以及图像的属性exif信息等.CGImageDestinationRef可以将数据写到图片中如exif信息
二、CGImageSourceRef的使用
- (void)getdata:(NSString*)imagePath { // 传入图片的路径x
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:imagePath],NULL);
NSDictionary * imageProperty=(NSDictionary*)CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
NSDictionary* exifDictionary=[imageProperty valueForKey:(NSString*)kCGImagePropertyExifDictionary];//获取图片的exif信息
NSDictionary*tiffDictonary=[imageProperty valueForKey:(NSString*)kCGImagePropertyTIFFDictionary];//获取图片的tiff信息
CFStringRef fileUTI=CGImageSourceGetType(imageSource);
CFStringRef fileTypeDes = CFCopyDescription(fileUTI);
NSString* filetype=(NSString*)fileTypeDes;//获取图片的属性
}
三、CGImageDestinationRef的使用修改exif信息
- (void)saveExif:(NSString*)imagePath { // 传入图片的路径
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:imagePath],NULL);
NSDictionary *dict = (NSDictionary*)CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
NSMutableDictionary *dictInfo = [NSMutableDictionary dictionaryWithDictionary:dict];
CFStringRef imagUTI = CGImageSourceGetType(imageSource);
CGImageDestinationRef _imageDestination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:imagePath], imagUTI, 1, NULL);
// modify dict before add
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY:MM:dd hh:mm:ss"];
NSString *now = [formatter stringFromDate:[NSDate date]];
//设置图片的拍摄日期
[exifDictInfo setObject:now forKey:(NSString*)kCGImagePropertyExifDateTimeOriginal];
[exifDictInfo setObject:now forKey:(NSString*)kCGImagePropertyExifDateTimeDigitized];
// modify orientation
[dictInfo setValue:exifDictInfo forKey:(NSString*)kCGImagePropertyExifDictionary];
CGImageDestinationAddImageFromSource(_imageDestination, imageSource, 0, (CFDictionaryRef)dictInfo);
CGImageDestinationFinalize(_imageDestination);
}
}
这里只修改了图片的拍摄时间,大家可以试着去修改图片的方向等信息
[dictInfo setValue:[NSNumber numberWithInteger:newImageOrientation]forKey:(NSString*)kCGImagePropertyOrientation];
NSInteger currentOrientation = [[dict valueForKey:(NSString*)kCGImagePropertyOrientation] intValue];//获取当前的方向