无法使用自定义元数据保存图像

时间:2012-11-12 17:03:19

标签: ios assetslibrary

我正在尝试在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;
    };
}

libraryALAssetsLibrary个实例,saveImage: toAlbum:方法来自this blog post,只是稍加修改,以便我可以保存元数据:

-(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。图片已成功保存到正确的相册,只是缺少元数据。

我在保存/加载过程中做错了什么?实际上我不允许将自定义元数据保存到图像中吗?

1 个答案:

答案 0 :(得分:5)

我做了一些测试,据我所知,简短的回答是'你不能那样做'。看起来元数据必须符合特定的EXIF元数据键。您可以查找可用的TIFF元数据键,并查看是否有任何要设置/覆盖的值。例如,您可以尝试使用kCGImagePropertyTIFFImageDescription来存储数据。

NSMutableDictionary *tiffDictionary= [[NSMutableDictionary alloc] init];
NSMutableDictionary *myMetadata = [[NSMutableDictionary alloc] init];
[tiffDictionary setObject:@"My Metadata" forKey:(NSString*)kCGImagePropertyTIFFImageDescription];
[myMetadata setObject:tiffDictionary forKey:(NSString*)kCGImagePropertyTIFFDictionary];

...并使用图片保存myMetadata。

对于其他键,请看: http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGImageProperties_Reference/Reference/reference.html

否则,我要做的是创建一个NSDictionary,它使用图像的唯一标识符作为键,并将元数据对象存储为值。保存/加载图像时保存/加载此NSDictionary。