2011-07-28 59 views
0

我使用的方法writeImageToSavedPhotosAlbum:元數據:completionBlock:拍攝照片保存到相冊,代碼爲:writeImageToSavedPhotosAlbum:元數據:completionBlock:

-(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); 
      } 
    } 
} 

然後記錄元數據爲空。這是爲什麼?我如何獲取我保存的元數據信息?謝謝!

回答

0

只有當sourceType是UIImagePickerControllerSourceTypeCamera時,圖像的元數據纔可用。

See Ref。看看該頁面的最後一段。

+0

實話告訴你,我知道,可作爲sourceType的圖像的元數據UIImagePickerControllerSourceTypeCamera.But我不知道如何獲得元數據信息?哪種方法可以使用? – scofield

0

你可以把元數據的日誌,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]; 
    } 
} 
... 
}