2013-10-24 84 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。 –

相關問題