My app crashes every time when I try to save image using photo framework.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
_mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
_mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
PHObjectPlaceholder *assetPlaceholder = _mChangeRequest.placeholderForCreatedAsset;
}
else {
NSLog(@"write error : %@",error);
}
}];
}
crash : NSInternalInconsistencyException', reason: 'This method can only be called from inside of -[PHPhotoLibrary performChanges:completionHandler:] or -[PHPhotoLibrary performChangesAndWait:error:]'
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:
In case you need to do something with the placeholder of the newly created asset, you can access it inside the same
performChanges
block:Here is an example of code how you can write/save image to Photo Library using
UIImageWriteToSavedPhotosAlbum
function:Don't forget to add into your
Info.plist
a key-value pairPrivacy - Camera Usage Description
with description of usage.In Swift 3 I do this to save the video to the library.
you will see that image in the photos app
you will change code probably like this...