2013-10-24 128 views
0

我正在使用一个简单的益智游戏,使用图像作为棋盘游戏的背景。 一切工作正常,但有一件事情困扰着我。当我从相机拍摄照片时,我可以将其保存在我的照片库中,但我不知道它保存在哪里,我的意思是,照片的确切网址是什么。如何知道我的照片库中保存的照片的网址?

这里就是我将照片保存代码:

- (void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *) info 
{ 
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType]; 
    UIImage *originalImage, *editedImage, *imageToSave; 

    // Handle a still image capture 
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) { 

     editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage]; 
     originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage]; 

     if (editedImage) { 
      imageToSave = editedImage; 
     } else { 
      imageToSave = originalImage; 
     } 

     CGRect bounds; 

     bounds.origin = CGPointZero; 
     bounds.size = imageToSave.size; 

     [scrollView setContentSize:bounds.size]; 
     [scrollView setContentOffset:bounds.origin]; 
     [imageView setFrame:bounds]; 
     imageView.image = imageToSave; 

     if ([info objectForKey:@"UIImagePickerControllerReferenceURL"] == nil) { 
      // Save the new image (original or edited) to the Camera Roll 
      ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init]; 
      ALAssetOrientation orientation; //= [[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] integerValue]; 
      NSString *infoOrientation = [[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"]; 
      switch ([infoOrientation integerValue]) { 
       case 3: 
        orientation = ALAssetOrientationUp; 
        break; 
       case 6: 
        orientation = ALAssetOrientationRight; 
        break; 
       default: 
        orientation = ALAssetOrientationDown; 
        break; 
      } 
      [al writeImageToSavedPhotosAlbum:[imageToSave CGImage] orientation:orientation completionBlock:^(NSURL *assetURL, NSError *error) { 
       if (error == nil) { 
        NSLog(@"saved"); 
        savedImageCam = assetURL; 
       } else { 
        NSLog(@"error"); 
       } 
      }]; 
     } else { 
      NSLog(@"URL from saved image: %@", savedImageCam); 
      NSLog(@"URL from photo image: %@", [info objectForKey:@"UIImagePickerControllerReferenceURL"]); 
     } 
    } 

    // Handle a movie capture 
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) { 

     NSString *moviePath = (NSString *)[[info objectForKey:UIImagePickerControllerMediaURL] path]; 

     if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) { 
      UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil); 
     } 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

调用方法UIImageWriteToSavedPhotosAlbum没有返回URL,并将实例变量info只要将URL,如果我通过直接调用图片库不相机。

任何人都知道我该如何解决它?

+0

你真的想把图像保存到照片库吗?为什么不将图像保存在应用程序的沙箱中以备将来参考? – rmaddy

+0

是的,我真的需要保存在照片库中。 –

回答

0

请勿使用UIImageWriteToSavedPhotosAlbum。而应使用ALAssetsLibrarywriteImageToSavedPhotosAlbum:orientation:completionBlock:。然后,completionBlock可让您访问您可以用来在未来再次获取图片的资源网址。

+0

非常感谢@Wain,请查看我的代码上的更改。现在我可以从保存的照片中知道URL。 –

相关问题