1

我正在尝试在我的 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使用相机的图像。图片已成功保存到正确的相册,只是缺少元数据。

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

4

1 回答 1

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。

于 2012-11-19T14:20:38.097 回答