Trying hard to get UIImageWriteToSavedPhotosAlbum to work in swift https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/index.html#//apple_ref/c/func/UIImageWriteToSavedPhotosAlbum
The documentation is sadly ONLY in objective C.
Here is my code:
func saveImage()
{
UIImageWriteToSavedPhotosAlbum(uiimage, self, "saveImageComplete:::", nil)
}
func saveImageComplete(image:UIImage,err:NSError,context:UnsafePointer<()>)
{
loadLastPhotoIntoGalleryIcon()
}
But the problem is that it throws the NSInvalidArgumentException with an unrecognized selector:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'app.PhotoEditor<0x14a1e400> does not respond to selector
saveImageComplete:::'
Can you advise what is wrong with my syntax and how I properly specific this selector? From what I understand, each : represents 1 argument the method expects and since it has 3 parameters I gave it 3 :'s.
Thanks!
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.
In Swift 4.x and 5.0:
UIImageWriteToSavedPhotosAlbum(imageToSave, self, #selector(saveImageComplete(image:err:context:)), nil)
Don't forget the @objc
decorator in it's declaration:
@objc private func saveImageComplete(image:UIImage, err:NSError, context:UnsafeMutableRawPointer?) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With