UIImageWriteToSavedPhotos Select Album Selector Syntax

Trying to get UIImageWriteToSavedPhotosAlbum to work in quick https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/index.html#//apple_ref/c/func/UIImageWriteToSavedPhotosAlbum

The documentation is sad ONLY in lens 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 an 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 correctly defined this selector? From what I understand, each one: represents 1 argument that the method expects, and since it has 3 parameters, I gave it 3: s.

Thanks!

+7
source share
5 answers

If your method was Objective-C, the selector would be like "saveImageCompleteImage: err: context:". You must remember that parameters are part of the name in Objective-C, so "saveImageComplete:::" does not indicate a method that could be called saveImageComplete(image:UIImage,err:NSError,context:UnsafePointer<()>) in Swift .

+4
source

Nothing above works for Swift3 . Try this for anyone struggling with Swift3 Syntax's

Act:

 @IBAction func saveToPhotos(_ sender: AnyObject) { UIImageWriteToSavedPhotosAlbum(yourImageView.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) } 

Goal:

 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) } } 

See this link for more details. https://www.hackingwithswift.com/read/13/5/saving-to-the-ios-photo-library

+12
source

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") } 
+4
source

You can do this by calling one line of code as well

  UIImageWriteToSavedPhotosAlbum(imageToSave!,self,nil,nil) 
+1
source

In Swift 4.x and 5.0:

 UIImageWriteToSavedPhotosAlbum(scrollView.screenshot()!, self, #selector(saveImageComplete(image:err:context:)), nil) 

Don't forget the @objc decorator @objc :

 @objc private func saveImageComplete(image:UIImage, err:NSError, context:UnsafeMutableRawPointer?) { } 
+1
source

Source: https://habr.com/ru/post/979357/


All Articles