Save image to photo library using photo framework

前端 未结 4 593
眼角桃花
眼角桃花 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 01:00

    All you need to do is trigger a creation request. As the error says, you can access the change request only inside the performChanges block.

    So to save the image you would do something like this:

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
             NSLog(@"Success");
        }
        else {
            NSLog(@"write error : %@",error);
        }
    }];
    

    In case you need to do something with the placeholder of the newly created asset, you can access it inside the same performChanges block:

    PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
    PHObjectPlaceholder *assetPlaceholder = changeRequest.placeholderForCreatedAsset;
    

提交回复
热议问题