将照片保存到相机胶卷并确保其实际保存

时间:2013-07-18 23:12:35

标签: ios objective-c image save photo

我目前正以这种方式将UIImage保存到相机胶卷。

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

但是如果用户拒绝我们访问他们的照片的权限会发生什么...我怎么能告诉你这已经发生并显示错误信息?

1 个答案:

答案 0 :(得分: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
    }