'CIImage returning nil in swift

I have a PKCanvas, what i am trying to do is to take the pkcanvas drawing that the user has made ad compare to another image. However i keep getting nil returned section. Why is this? my drawing and picture elements are returning nil. but my image2 element is returning the users drawing as expected. in the code below i am just comparing the drawing the user made against itself (just for testing purposes) Also "nothing here" does get printed.

//function type for ciimage
    func featureprintObservationForImage(_ image: CIImage?) -> VNFeaturePrintObservation? {
        guard let ciImage = image else {
            print("nothing here")
            return nil
        }
        let requestHandler = VNImageRequestHandler(ciImage: ciImage, options: [:])
        let request = VNGenerateImageFeaturePrintRequest()
        do {
          try requestHandler.perform([request])
          return request.results?.first as? VNFeaturePrintObservation
        } catch {
          print("Vision error: \(error)")
          return nil
        }
      }

@IBAction func finishCalculation(_ sender: Any) {
    
    //geting drawing
    UIGraphicsBeginImageContextWithOptions(theCanvasView.bounds.size, false, UIScreen.main.scale)
    theCanvasView.drawHierarchy(in: theCanvasView.bounds, afterScreenUpdates: true)
    
    let image2 = UIGraphicsGetImageFromCurrentImageContext()
   UIGraphicsEndImageContext()
    
    
    let picture = featureprintObservationForImage(image2?.ciImage)
    let drawing = featureprintObservationForImage(image2?.ciImage)
    

    var distance = Float(0)
        
        do {
            try picture!.computeDistance(&distance, to: drawing!)
        } catch {
            print(error)
        }
    
        print("picture to drawing")
        print(distance)

}


Sources

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

Source: Stack Overflow

Solution Source