'NSException when trying to save to camera roll

What I am trying to do it to save what's on my 'canvasView' to photos when a UIButton is pressed. However I am getting the following:

ERROR

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[drawingskills.ViewController saveDrawingToCamera:]: unrecognized selector sent to instance 0x7ff67bd04b60'
terminating with uncaught exception of type NSException

The steps I took are as follows:

STEP 1

Create UIBtton and link it to a function called saveDrawingToCameraRoll

STEP 2

set up the function as below:

func saveDrawingToCameraRoll(_ sender: Any) {
            UIGraphicsBeginImageContextWithOptions(canvasView.bounds.size, false, UIScreen.main.scale)
            canvasView.drawHierarchy(in: canvasView.bounds, afterScreenUpdates: true)
            
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            if image != nil {
                PHPhotoLibrary.shared().performChanges({
                    PHAssetChangeRequest.creationRequestForAsset(from: image!)
                })   
            }   
    }

STEP 3

Info.plist -> right click -> add row ->select "privacy - photo library usage description" from drop down

However this all results in the error mentioned at the start of this text.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source