writeImageToSavedPhotosAlbum:metadata:completionBlock:

Posted

技术标签:

【中文标题】writeImageToSavedPhotosAlbum:metadata:completionBlock:【英文标题】: 【发布时间】:2011-07-28 05:24:15 【问题描述】:

我使用方法将拍照保存到相册,代码为:

-(void)savePhotoToAlbum    
   CGImageRef imageRef=[imageView image].CGImage;

NSDictionary *currentDic=[self getLocation];
NSDictionary *metadata=[NSDictionary dictionaryWithDictionary:currentDic];

ALAssetsLibrary *library=[[ALAssetsLibrary alloc] init];

[library writeImageToSavedPhotosAlbum:imageRef metadata:metadata completionBlock:^(NSURL *assetURL,NSError *error)
   if(error == nil) 
    
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:nil message:@"Save success!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
     
    else 
    
        UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:nil message:@"Save failure!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    
];
[library release];

.getLocation方法是获取用户当前位置!这样可以保存成功!然后我想从相册中挑选照片使用UIImagePickerController!代码是:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
   if([picker sourceType]==UIImagePickerControllerSourceTypeSavedPhotosAlbum)//picker image delegate
    
        NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
        if([mediaType isEqualToString:@"public.image"])
        
            NSDictionary *metadata=[info objectForKey:UIImagePickerControllerMediaMetadata];
            NSLog(@"%@",metadata);
            
    

那么日志元数据是空的。这是为什么呢?以及如何获取我保存的元数据信息?谢谢!

【问题讨论】:

【参考方案1】:

仅当 sourceType 为 UIImagePickerControllerSourceTypeCamera 时,图像的元数据才可用。

See Ref。查看该页面中的最后一段。

【讨论】:

说实话,我知道源类型为 UIImagePickerControllerSourceTypeCamera 的图像的元数据是 UIImagePickerControllerSourceTypeCamera。但我不知道如何获取元数据信息?可以用什么方法?【参考方案2】:

您可以使用 AssetsLibrary 框架记录元数据:

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info

...
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) 
    NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
    if (url) 
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
            CLLocation *location = [myasset valueForProperty:ALAssetPropertyLocation];

            NSLog(@"\n\n\n____________________________\n");
            NSLog(@"ORIENTATION: %@\n",[myasset valueForProperty:ALAssetPropertyOrientation]);
            NSLog(@"LOCATION: %@\n",[myasset valueForProperty:ALAssetPropertyLocation]);
            NSLog(@"DATE: %@\n",[myasset valueForProperty:ALAssetPropertyDate]);
            NSLog(@"Duration: %@\n",[myasset valueForProperty:ALAssetPropertyDuration]);
            NSLog(@"TYPE: %@\n",[myasset valueForProperty:ALAssetPropertyType]);
            NSLog(@"\n____________________________\n\n\n");

                            //take coordinates only
            CLLocationCoordinate2D coordinate = [location coordinate];
            strCoord = [NSString stringWithFormat:@"long: %f; lat: %f;", coordinate.latitude, coordinate.longitude];
            NSLog(@"%@", strCoord);
            // location contains lat/long, timestamp, etc
            // extracting the image is more tricky and 5.x beta ALAssetRepresentation has bugs!

        ;
        ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) 
            NSLog(@"cant get image - %@", [myerror localizedDescription]);
        ;
        ALAssetsLibrary *assetsLib = [[ALAssetsLibrary alloc] init];
        [assetsLib assetForURL:url resultBlock:resultblock failureBlock:failureblock];
    

...

【讨论】:

以上是关于writeImageToSavedPhotosAlbum:metadata:completionBlock:的主要内容,如果未能解决你的问题,请参考以下文章