如何在iOS中编辑图像元数据?

时间:2014-04-02 09:29:40

标签: ios iphone objective-c

我使用以下代码修改图库图片的元数据。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)mediaInfo
{
    ALAssetsLibrary *library =nil;
    void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset)
    {
        NSDictionary *metadata = asset.defaultRepresentation.metadata;
        NSLog(@"Image Meta Data: %@",metadata);
        NSMutableDictionary* dict=[[NSMutableDictionary alloc] initWithDictionary:metadata];
        [dict setValue:@"sdfdfsfsfsfdsfsf" forKey:(NSString*)kCGImagePropertyIPTCKeywords];
        UIImage *anImage = [mediaInfo valueForKey:UIImagePickerControllerOriginalImage];
        NSData *imageData = UIImageJPEGRepresentation(anImage, 1.0);
        [asset writeModifiedImageDataToSavedPhotosAlbum:imageData metadata:dict completionBlock:^(NSURL *assetURL, NSError *error) {

        }];

    };

    NSURL *assetURL = [mediaInfo objectForKey:UIImagePickerControllerReferenceURL];
    library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:assetURL
             resultBlock:ALAssetsLibraryAssetForURLResultBlock
            failureBlock:^(NSError *error) {
            }];

   }

但它不起作用。请帮忙。

1 个答案:

答案 0 :(得分:0)

这是一个最小的例子:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *imageToSave = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

    /* modify metadata dict */
    NSMutableDictionary *imageMetadata = [[info objectForKey: UIImagePickerControllerMediaMetadata] mutableCopy];
    NSLog(@"Inspect current metadata: %@", imageMetadata);
    NSDictionary *iptc = @{(id)kCGImagePropertyIPTCKeywords: @"awesome photo"};
    [imageMetadata setObject:iptc forKey:(id)kCGImagePropertyIPTCDictionary];

    /* Save to the Camera Roll with modified medatadata */
    [[ALAssetsLibrary new] writeImageToSavedPhotosAlbum:imageToSave.CGImage
                                 metadata:imageMetadata
                          completionBlock:NULL];
    [self performDismiss];
}

如果您使用元数据查看器(OS X的预览已经足够好),您会看到如下内容:

enter image description here

但是,正如pawan建议的那样,使用现有的库可以更轻松地完成它。谷歌周围,那里有很多选择。