SmartTree
SmartTree

Reputation: 1471

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?

Upvotes: 3

Views: 972

Answers (2)

J.Doe
J.Doe

Reputation: 366

Check the strokes count:

if canvas.drawing.strokes.count == 0 {
    //empty drawing
}

Upvotes: 1

Maaz Fishermen Labs
Maaz Fishermen Labs

Reputation: 108

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 
    }

Upvotes: 7

Related Questions