我使用AVFoundation
创建了一个自定义相机,现在在捕获图像之后,需要将其保存在iPhone的图库中。
我尝试用UIImageWriteToSavedPhotosAlbum
保存图像,但是发现它不保存EXIF信息。
答案 0 :(得分:0)
要保存带有图像的EXIF信息,请参考以下代码。
- (void)saveImageDataToPhotoAlbum:(NSData *)originalData
{
NSDictionary *dataDic = [self getDataAndMetadata:originalData];
ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
[assetsLib writeImageDataToSavedPhotosAlbum:dataDic[@"data"]
metadata:dataDic[@"metadata"]
completionBlock:^(NSURL *url, NSError *e) {
[self addToMyAlbum:url];
}];
}
- (NSDictionary *)getDataAndMetadata:(NSData *)originalData
{
CGImageSourceRef cimage = CGImageSourceCreateWithData((CFDataRef)originalData, nil);
NSDictionary *metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(cimage, 0, nil);
NSMutableDictionary *metadataAsMutable = [NSMutableDictionary dictionaryWithDictionary:metadata];
metadataAsMutable[(NSString *)kCGImagePropertyGPSDictionary] = self.myGpsDic;
NSMutableData *dataForMetadataRemoval = [NSMutableData data];
CGImageDestinationRef dest =
CGImageDestinationCreateWithData((CFMutableDataRef)dataForMetadataRemoval, CGImageSourceGetType(cimage), 1, nil);
CGImageDestinationAddImageFromSource(dest, cimage, 0, (CFDictionaryRef)metadataAsMutable);
CGImageDestinationFinalize(dest);
CFRelease(dest);
[metadata release];
CFRelease(cimage);
return @{ @"data" : (dataForMetadataRemoval), @"metadata" : metadataAsMutable};
}
return metadataAsMutable;
}