2

According to the docs, PKDrawing conforms to Equatable. But if you compare 2 blank drawings with ==, it returns false. I submitted a bug via the feedback app, but I am posting here hoping I missed something or others will also submit a bug report so this can get fixed. I need to check if a PKCanvasView has any content, and being that PKDrawing is Opaque we can't query for strokes or other data. Given the limited api, it seems that the best way to check would be something like this:

extension PKCanvasView {
    func isEmpty() -> Bool {
      return self.drawing == PKDrawing()
    }
}

This will return false though regardless of the canvasView.drawing. Even, PKDrawing() == PKDrawing() returns false.

byaruhaf
  • 4,128
  • 2
  • 32
  • 50
hidden-username
  • 2,610
  • 3
  • 14
  • 19
  • 1
    Oddly the documentation also shows that it implements a `!=` function and the documentation states that it return `true` if the objects are equal. Seems backwards. – rmaddy Aug 20 '19 at 02:36

2 Answers2

3

In this case, you can check bounds of drawing object. And iOS 14 has provided strokes that this drawing contains.

extension PKDrawing {
    
    func isEmpty() -> Bool {

        if #available(iOS 14.0, *) {
            return strokes.isEmpty
        } else {
            return bounds.isEmpty
        }
    }
}
Bhuvan Bhatt
  • 3,276
  • 2
  • 18
  • 23
Nim Thitipariwat
  • 778
  • 6
  • 12
0

This is my approach to check if a drawing is blank:

extension PKDrawing {
    var isBlank: Bool {
        get {
             return self.bounds == CGRect(origin: CGPoint(x: CGFloat.infinity, y: CGFloat.infinity), size: .zero)
        }
    }
}
Michael
  • 57
  • 2
  • I like this concept but I'm getting TRUE on all drawings. I used canvasView.drawing.isBlank ? print("BLANK") : print("DRAWING") and even though the drawings were saved, self.bounds shows as (inf, inf, 0.0, 0.0) whether blank or not. I'm not sure bounds is updated. canvasView is my PKCanvasView. Do you know what I may be doing wrong if anything? – Marcy Jun 26 '20 at 02:42