UIImageWriteToSavedPhotosAlbum Selector Syntax Issue
Above none work for Swift3
. Try this for those who are struggling with Swift3 Syntax's
Action:
@IBAction func saveToPhotos(_ sender: AnyObject) {
UIImageWriteToSavedPhotosAlbum(yourImageView.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}
Target:
func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
if error == nil {
let ac = UIAlertController(title: "Saved!", message: "Image saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(ac, animated: true, completion: nil)
} else {
let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(ac, animated: true, completion: nil)
}
}
Refer this link for more clarification https://www.hackingwithswift.com/read/13/5/saving-to-the-ios-photo-library
The correct UIImageWriteToSavedPhotosAlbum/selector code is here:
func onYesClicked(action:Int){
// i'm using optional image here just for example
if let image = getImage() {
UIImageWriteToSavedPhotosAlbum(
image, self,
Selector("image:didFinishSavingWithError:contextInfo:"),
nil)
}
}
func image(
image: UIImage!,
didFinishSavingWithError error:NSError!,
contextInfo:UnsafePointer<Void>)
{
// process success/failure here
}
Here's the most up-to-date syntax, 2016
@IBAction func clickedSaveToUsersCameraRoll()
{
print("actually saving yourImage.image to the camera roll, 2016.")
UIImageWriteToSavedPhotosAlbum(
yourImage.image!, self,
#selector(savedOK(_:didFinishSavingWithError:contextInfo:)),
nil)
}
func savedOK(
image:UIImage!,
didFinishSavingWithError error:NSError!,
contextInfo:UnsafePointer<Void>)
{
print("Wrote photo ok")
}
If your method were an Objective-C method, the selector would be something like "saveImageCompleteImage:err:context:". You need to remember that the parameters are part of the name in Objective-C, so "saveImageComplete:::"
doesn't specify a method that could be called saveImageComplete(image:UIImage,err:NSError,context:UnsafePointer<()>)
in Swift.