swiftuiimageviewuilongpressgesturerecogni

How can i resolve this Error :unrecognised selector sent to instance (LongPress onUIImageView)


reason: '-[HMIS_ios.ImageViewerVController longPressed:]: unrecognized selector sent to instance 0x7fe865576d70'

class ImageViewerVController: UIViewController {

@IBOutlet weak var ImageViewer: UIImageView!
var pass = ""
override func viewDidLoad() {
    super.viewDidLoad()
    print("URL:" + pass)
    self.ImageViewer.sd_setImage(with: URL(string:"\(pass)"))

    ImageViewer.isUserInteractionEnabled = true
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("longPressed:")))
    longPressRecognizer.minimumPressDuration = 0.5
    self.ImageViewer.addGestureRecognizer(longPressRecognizer)
}



@objc func longPressed(sender: UILongPressGestureRecognizer) {
        UIImageWriteToSavedPhotosAlbum(ImageViewer.image!, self, Selector(("image:didFinishSavingWithError:contextInfo:")), nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

I want to Save Image in Gallery using UILongPressGestureRecognizer


Solution

  • Change

    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("longPressed:")))
    UIImageWriteToSavedPhotosAlbum(ImageViewer.image!, self, Selector(("image:didFinishSavingWithError:contextInfo:")), nil)
    

    to

    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(sender:)))
    UIImageWriteToSavedPhotosAlbum(ImageViewer.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
    

    Change didFinishSavingWithError method like this

    @objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
        if let error = error {
            print(error.localizedDescription)
        } else {
            print("image saved")
        }
    }