2012-02-09 92 views
4

我正在編寫一個應用程序,讓用戶拍照並將其分配給設備。爲此,我需要能夠存儲拍攝圖片的URL並在之後重新加載圖片。如何使用ALAsset保存並重新加載圖像?

我知道已經有上多次討論,但奇怪的是沒有Ansers的幫我...

因此,這裏是我的保存圖像代碼:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init]; 
    UIImage * image; 

    // Request to save Image 
    if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) { 
     image = [info objectForKey:UIImagePickerControllerEditedImage]; 
     [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL * assertURL, NSError * error){ 
      if (error) { 
       UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"PictureSaveErrorTitle", @"Title if saving picture fails") 
                   message:NSLocalizedString(@"PictureSaveErrorMessage", @"Message if saving picture fails") 
                   delegate:nil 
                 cancelButtonTitle:NSLocalizedString(@"PictureSaveErrorOK", @"OK if saving picture fails") 
                 otherButtonTitles:nil]; 
       [alert show]; 
      } else { 
       [self setUserImage:[assertURL absoluteString]]; 
       [imageOfDevice setImage:image]; 
      } 
     }]; 
    } else { 
     image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     [self setUserImage:[[info valueForKey:UIImagePickerControllerReferenceURL] path]]; 
    } 

    [self dismissModalViewControllerAnimated:YES]; 
} 

這是重裝圖片:

if (userImage != nil) { 
     ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init]; 
     [library assetForURL:[NSURL fileURLWithPath:userImage] 
       resultBlock:^(ALAsset * asset){ 
        [imageOfDevice setImage:[UIImage imageWithCGImage:[asset thumbnail]]]; 
       } 
       failureBlock:^(NSError * error){ 
        NSLog(@"cannot get image - %@", [error localizedDescription]); 
       }]; 
} 

有人看到有什麼毛病嗎?

感謝您的幫助,

sensslen

+0

我仍然在尋找任何人有一個偉大的想法來幫助我我仍然堅持這個愚蠢的問題。其實這並不難。有大量的應用程序在圖片庫中做些什麼,不在嗎? – sensslen 2012-02-13 07:38:50

回答

7

簡單的解決辦法是要傳遞的absoluteString來代替NSURL。

此代碼適用於我在picker中完成。

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
if([picker sourceType] == UIImagePickerControllerSourceTypeCamera) 
{ 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    [library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) 
    { 
     NSLog(@"IMAGE SAVED TO PHOTO ALBUM"); 
     [library assetForURL:assetURL resultBlock:^(ALAsset *asset) 
     { 
      NSLog(@"we have our ALAsset!"); 
     } 
       failureBlock:^(NSError *error) 
     { 
      NSLog(@"Error loading asset"); 
     }]; 
    }]; 
} 
+0

實際上,我決定將圖片保存到應用程序特定的目錄,現在正在工作。儘管如此,我嘗試了你的代碼。我能夠保存圖像 - 這不是問題。問題是重新加載它...我無法接收圖像的縮略圖...您能否顯示加載圖像的代碼? – sensslen 2012-02-21 13:33:38

+1

只需替換NSLog(「我們有我們的ALAsset!」);與 UIImage * image = [UIImage imageWithCGImage:[asset defaultRepresentation] .fullResolutionImage]; – 2012-02-21 19:33:21

+0

嗯,聽起來很簡單...我以爲我曾嘗試過,但最有可能的是我做了其他的事情....儘管如此,感謝您的幫助! – sensslen 2012-02-27 08:00:32

相關問題