Save a photo to the camera roll and make sure it actually saved

泄露秘密 提交于 2019-12-19 04:22:19

问题


I am currently saving a UIImage to the camera roll this way.

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

But what happens if the user denies us permission to access their photos... how can I tell that this has happened and display an error message?


回答1:


To save the image to the camera roll I'm Using ALAssetsLibrary so in the method:

//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
}

Also remember that you have to check first if the camera is available.

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


来源:https://stackoverflow.com/questions/17735543/save-a-photo-to-the-camera-roll-and-make-sure-it-actually-saved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!