I can take a picture with phone save it on gallery, but I don't know how can stock the path of picture, so after I can retrieve it from gallery (I don't know yet how retrieve a picture by his path) to show it on my controller.
I have this code which works fine. I can see my image saved on gallery but I don't know how to get his url, witch I want to save on coredata
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
exerciceImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage
exerciceImage.isHidden = false;
UIImageWriteToSavedPhotosAlbum(exerciceImage.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let name = image.accessibilityIdentifier;
let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
I read that I can use PHPhotoLibrary, but it give me error that it can't cast to nsurl Here is what I tried
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
imagePicker.dismiss(animated: true, completion: nil)
exerciceImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage
exerciceImage.isHidden = false;
PHPhotoLibrary.shared().performChanges({
let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: info[UIImagePickerControllerOriginalImage] as! URL)
let assetPlaceholder = assetRequest?.placeholderForCreatedAsset
}, completionHandler: { success, error in
// completion handling
let url = info[UIImagePickerControllerOriginalImage] as! URL;
})
}