我正在尝试在我的 iOS 应用程序中创建自定义图片库。我想让用户能够将某些元数据与图像一起保存,以便稍后可以在应用程序中使用附加信息将其拉出。
首先,当用户拍照时,应用程序将图像保存到应用程序的自定义相册中:
UITextField *nameField = [alertView textFieldAtIndex:0];
NSMutableDictionary *metaData = [[NSMutableDictionary alloc] init];
[metaData setObject:currentEvent forKey:kMetaDataEventKey];
[metaData setObject:[AppDelegate getActivePerson].name forKey:kMetaDataPersonKey];
[metaData setObject:nameField.text forKey:kMetaDataNameKey];
NSLog(@"Saving image with metadata: %@", metaData);
NSMutableDictionary *realMetaData = [[NSMutableDictionary alloc] init];
[realMetaData setObject:metaData forKey:kCGImagePropertyTIFFDictionary];
[library saveImage:imageToSave toAlbum:albumName metadata:realMetaData withCompletionBlock:^(NSError *error) {
if ( error != nil )
{
NSLog(@"Error saving picture? %@", error);
}
[self.tableView reloadData];
}];
保存后,我收到以下日志消息:
Saving image with metadata: {
Event = t;
Person = "George James";
PictureName = tt;
}
然后当我稍后尝试检索图像时,我使用此功能
-(void) loadAssets
{
self.assets = [NSMutableArray arrayWithCapacity:album.numberOfAssets];
[album enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if ( result != nil )
{
NSDictionary *metaData = result.defaultRepresentation.metadata;
NSLog(@"Retrieved image metadata: %@", metaData);
}
else
{
[self.tableView reloadData];
}
}];
}
但是日志显示它没有成功保存与图像关联的元数据:
Retrieved image metadata: {
ColorModel = RGB;
DPIHeight = 72;
DPIWidth = 72;
Depth = 8;
Orientation = 1;
PixelHeight = 720;
PixelWidth = 960;
"{Exif}" = {
ColorSpace = 1;
ComponentsConfiguration = (
1,
2,
3,
0
);
ExifVersion = (
2,
2,
1
);
FlashPixVersion = (
1,
0
);
PixelXDimension = 960;
PixelYDimension = 720;
SceneCaptureType = 0;
};
"{TIFF}" = {
Orientation = 1;
ResolutionUnit = 2;
XResolution = 72;
YResolution = 72;
"_YCbCrPositioning" = 1;
};
}
library
是一个ALAssetsLibrary
实例,saveImage: toAlbum:
方法来自这篇博文,只是稍作修改,以便我可以这样保存元数据:
-(void)saveImage:(UIImage *)image toAlbum:(NSString *)albumName metadata:(NSDictionary *)metadata withCompletionBlock:(SaveImageCompletion)completionBlock
{
//write the image data to the assets library (camera roll)
[self writeImageToSavedPhotosAlbum:image.CGImage
metadata:metadata
completionBlock:^(NSURL* assetURL, NSError* error) {
//error handling
if (error!=nil) {
completionBlock(error);
return;
}
//add the asset to the custom photo album
[self addAssetURL: assetURL
toAlbum:albumName
withCompletionBlock:completionBlock];
}
];
}
图像来自UIImagePickerController
使用相机的图像。图片已成功保存到正确的相册,只是缺少元数据。
我在保存/加载过程中做错了吗?我实际上不允许将自定义元数据保存到图像吗?