Save image to photo library using photo framework

前端 未结 4 597
眼角桃花
眼角桃花 2020-12-16 00:51

My app crashes every time when I try to save image using photo framework.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMedi         


        
4条回答
  •  天命终不由人
    2020-12-16 00:58

    Here is an example of code how you can write/save image to Photo Library using UIImageWriteToSavedPhotosAlbum function:

    - (void)saveImage:(UIImage *)image {
        UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
    {
        if (!error) {
            // saved successfully
    
            PHFetchOptions *fetchOptions = [PHFetchOptions new];
            fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
            PHAsset *asset = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions].firstObject;
            if (asset != nil) {
                // here you can use asset of your image
            }
        } else {
            NSLog(@"save image error: %@", error);
        }
    }
    

    Don't forget to add into your Info.plist a key-value pair Privacy - Camera Usage Description with description of usage.

提交回复
热议问题