使用Photokit使用元数据编写照片 [英] Writing a Photo with Metadata using Photokit

查看:137
本文介绍了使用Photokit使用元数据编写照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用ALAsset框架将图片从Photo库保存到包含元数据的文档目录。我使用的代码是

I am currently using ALAsset framework for saving an image from Photo library to documents directory with metadata. The code I use is

ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:[NSURL URLWithString:miv.assetURL] resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *image_representation = [asset defaultRepresentation];
        CGImageSourceRef source = Nil;
        uint8_t *buffer = (Byte*)malloc(image_representation.size);
        NSUInteger length = [image_representation getBytes:buffer fromOffset: 0.0  length:image_representation.size error:nil];
        if (length != 0)  {
          NSData *adata = [[NSData alloc] initWithBytesNoCopy:buffer length:image_representation.size freeWhenDone:YES];              
          NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:(id)[image_representation UTI] ,kCGImageSourceTypeIdentifierHint,nil];              
          // create CGImageSource with NSData
          source = CGImageSourceCreateWithData((__bridge CFDataRef) adata,  (__bridge CFDictionaryRef) sourceOptionsDict);              
        }
        NSDictionary *metadata = [image_representation metadata];
        NSMutableDictionary *metadataAsMutable = [metadata mutableCopy];
        NSMutableDictionary *EXIFDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary];
        NSMutableDictionary *GPSDictionary = [metadataAsMutable objectForKey:(NSString *)kCGImagePropertyGPSDictionary];
        if(!EXIFDictionary) {
          EXIFDictionary = [NSMutableDictionary dictionary];
        }
        if(!GPSDictionary) {
          GPSDictionary = [NSMutableDictionary dictionary];
        }
        [metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
        [metadataAsMutable setObject:GPSDictionary forKey:(NSString *)kCGImagePropertyGPSDictionary];
        CFStringRef UTI = CGImageSourceGetType(source);
        NSMutableData *dest_data = [NSMutableData data];
        CGImageDestinationRef destination CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,UTI,1,NULL);                        
        CGImageDestinationAddImageFromSource(destination,source,0, (__bridge CFDictionaryRef) metadataAsMutable);
        BOOL success = NO;
        success = CGImageDestinationFinalize(destination);

        if(!success) {
        }
        [dest_data writeToFile:myImageFileName atomically:YES];
        CFRelease(destination);
        CFRelease(source);
}];

我想将此代码转换为使用Photo Kit。根据我的研究,我发现Photo Kit处理写入的方式是

I want to convert this code to use Photo Kit. From my research I found the way Photo kit will handle a write is

PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObjects:imageURLString, nil] options:nil];
[savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
[[PHImageManager defaultManager]
     requestImageForAsset:(PHAsset *)asset
     targetSize:desiredSize
     contentMode:PHImageContentModeAspectFit
     options:Nil
     resultHandler:^(UIImage *result, NSDictionary *info) {
         // Write Here
         // The info here has no EXIF or Metadata. So get them
         PHContentEditingInputRequestOptions *editOptions =      
         [[PHContentEditingInputRequestOptions alloc]init];
         editOptions.networkAccessAllowed = YES;
         [asset requestContentEditingInputWithOptions:editOptions completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
             CIImage *image = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];
             NSLog(@"metadata: %@", image.properties.description);
             ?? How do I write this to doc folder ?
       }];

     }
}


推荐答案

经过一番研究,我发现了这一点。以下是使用Exif将图像保存到文档目录的方法。

I figured it out after some research. Here is how you save image to Document Directory with Exif.

PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObjects:myAssetURL, nil] options:nil];
        [savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
          PHImageRequestOptions *cropToSquare = [[PHImageRequestOptions alloc] init];
          cropToSquare.synchronous = YES;
          [[PHImageManager defaultManager] requestImageDataForAsset:asset
                                                            options:cropToSquare
                                                      resultHandler:
           ^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
             CIImage* ciImage = [CIImage imageWithData:imageData];
             NSMutableDictionary *metadataAsMutable = [ciImage.properties mutableCopy];
             CGImageSourceRef source = Nil;
             NSDictionary* sourceOptionsDict = [NSDictionary dictionaryWithObjectsAndKeys:dataUTI ,kCGImageSourceTypeIdentifierHint,nil];
             source = CGImageSourceCreateWithData((__bridge CFDataRef) imageData,  (__bridge CFDictionaryRef) sourceOptionsDict);
             CFStringRef UTI = CGImageSourceGetType(source);
             NSMutableData *dest_data = [NSMutableData data];
             CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data,UTI,1,NULL);
             CGImageDestinationAddImageFromSource(destination,source,0, (__bridge CFDictionaryRef) metadataAsMutable);
             BOOL success = NO;
             success = CGImageDestinationFinalize(destination);

             if(!success) {
             }
             [dest_data writeToFile:myTrumbImageFileName atomically:YES];
             CFRelease(destination);
             CFRelease(source);
           }];
        }];

这篇关于使用Photokit使用元数据编写照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆