2013-07-18 103 views
2

我正在以這種方式將UIImage保存到相機膠捲。將照片保存到相機膠捲並確保其實際保存

UIImageWriteToSavedPhotosAlbum(finalPicture.image, nil, nil, nil);

但是,如果用戶拒絕允許我們訪問他們的照片會發生什麼......我怎麼能告訴,這已經發生了,顯示錯誤消息?

+0

您是否嘗試傳遞完成處理函數並查看會發生什麼? – rmaddy

+0

@rmaddy我不知道有一個:)完成處理程序的代碼是什麼?我會在哪裏實施這樣的事情? :) –

+0

您是否閱讀過「UIImageWriteToSavedPhotosAlbum」的文檔? – rmaddy

回答

6

要保存圖像的相機膠捲我使用ALAssetsLibrary這樣的方法:

//Called after taking the photo with the camera or selected the image from the gallery 
- (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

NSURL *referenceURL; 

if(Picker.sourceType==UIImagePickerControllerSourceTypeCamera){ 

    UIImage* takenImage=info[UIImagePickerControllerOriginalImage]; 

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
    // Request to save the image to camera roll 
    [library writeImageToSavedPhotosAlbum:[takenImage CGImage] orientation:(ALAssetOrientation)[takenImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){ 
     if (error) { 
      //NOT SAVED 
      //DISPLAY ERROR THE PICTURE CAN'T BE SAVED 
     } else { 
      //SAVED 
     } 
    }];   

} 
}else{ 
//Selected from gallery 
} 

還記得,你必須先檢查照相機。

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    { 
     //The camera is available 
    }else{ 
     //No camera available 
    } 
+0

但是,這仍然工作只是保存一個UIImage或這只是爲了讓他們與相機拍攝照片,然後保存? –

+0

@AlbertRenshaw如果最終使用'ALAssetsLibrary',您可以先調用'[ALAssetsLibrary authorizationStatus];'來確定當前狀態。 – rmaddy

+0

您可以在任何想要保存UIImage的地方使用ALAssetsLibrary,但在這種情況下,我會保存用戶使用相機拍攝的圖像,因此可以雙向工作。如果你想保存一個沒有用相機拍攝的UIImage,使用相同的方法,唯一的區別是你如何獲得UIImage * takenImage = @「myimage.png」;你不需要 - (void)imagePickerController:(UIImagePickerController *)Picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法,因爲你正在從另一個地方保存圖像。 – Moy

相關問題