0

我正在为 iOS 制作一个包含照片元数据的相机应用程序。我使用资产库来编写元数据。“writeImageDataToSavedPhotoAlbum”方法有一个返回资产 URL 的完成块。我假设我需要保存此 URL,以便检索元数据和图像。但是,我似乎无法弄清楚如何处理assetURL,以便获取我的信息。

我的代码:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(__bridge NSString *)kUTTypeMovie]){
    NSURL *urlOfVideo = [info objectForKey:UIImagePickerControllerMediaURL];
} else if ([mediaType isEqualToString:(__bridge NSString *)kUTTypeImage]) {

    CLLocation *location = [[CLLocation alloc] init];

    NSDictionary *metadata = [self getGPSDictionaryForLocation:location];

    UIImage *theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(theImage)];
    [library writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error){
        if (error) {
            NSLog(@"error");
        } else {

////what do I do with the assetURL?

            self.imageURL = assetURL;
            NSLog(@"url %@", self.imageURL);

        }
    } ];

}


[picker dismissViewControllerAnimated:YES completion:nil];
}
4

1 回答 1

0

您可以使用以下代码使用 ALAsset URL 加载图像

 UIImage *myImage = nil;

 ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
 {

    CGImageRef iref = [myasset aspectRatioThumbnail];         

   dispatch_sync(dispatch_get_main_queue(), ^{

       myImage = [UIImage imageWithCGImage:iref];

   });//end block


};
ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    NSLog(@"Cant get image - %@",[myerror localizedDescription]);
};
NSURL *asseturl = //assign your ALAsset URL here
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:asseturl 
               resultBlock:resultblock
              failureBlock:failureblock];
于 2013-02-11T03:02:43.147 回答