swiftuiimagepencilkit

How to determine if PKDrawing is blank?


I am using PencilKit in my app and let my users draw images on PKCanvasView. But I don't want them to be able to save blank drawings. So I need to check if PKDrawing is blank (doesn't contain anything besides transparent pixels). I couldn't find any specific method for it in the official documentation.

I've tried to convert the PKDrawing to Data and check if it is empty but it is never the case even when drawings are blank.

Is there a handy way to check if the PKDrawing or PKCanvasView are empty?

Alternatively, is there a way to check if UIImage consists only of transparent pixels?


Solution

  • You can simply check whether something has been drawing in

    if canvasView.drawing.bounds.isEmpty {
            print("nothing has been drawn so far")
        } else {
            // task which you can want to do with your drawing 
        }