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

2019-02-15 14:53发布

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条回答
Animai°情兽
2楼-- · 2019-02-15 15:12

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
    }
查看更多
登录 后发表回答