1

I have an app using PencilKit. I have noticed that occasionally when I am in dark mode and using the white colour, when I finish drawing and render the strokes into an image to draw, the white lines go to black.

I am using:

 currentTraits.performAsCurrent {
          image = drawing.image(from: visibleRect, scale: UIScreen.main.scale)
    }

to create a UIImage from the drawing but it randomly, occasionally seems to get the user interface style wrong. All the colours in the image are in the wrong userinterface style trait (but white is the most visibly wrong) (ie: I am in dark mode but the UIImage generated sometimes thinks I am in light mode)

Is this a bug in iOS (14.7.1 running on an original iPad Pro) or am I doing something wrong? I have never seen this happen in light mode (black lines never go to white).

Thanks in advance.


This does seem to be an iOS bug. It is present in iOS 14 but seems to be fixed in iOS 15.

  • How do you set color into PKInk? Could you show this code snippet? – Turtleeeeee Aug 24 '21 at 06:38
  • I am just using the PKCanvasView. That is working fine, I can draw whatever and it looks fine. I then use: drawingData = canvas.dataRepresentation() to store the drawing as Data. I store an array of that Data, then use: drawing = try PKDrawing(data: theDrawingData) to turn it back into a Drawing before doing the drawing.image call. The strange thing is I can have 20 items in my array and each time I re-image them to UIImage, different ones come out wrong. And only in dark mode. – NZ Programmer Aug 24 '21 at 21:02
  • 1
    Did you ever find a solution to this? I've noticed it happening also, but in the opposite direction (again only when I create a UIImage from the PKdrawing) – Gary Oct 05 '21 at 11:21
  • It seems completely fixed in iOS 15. – NZ Programmer Dec 12 '22 at 02:36

1 Answers1

2

You can do it with the help of PKDrawing extension by using traitCollection.performAsCurrent:

extension PKDrawing {
    func thumbnail(rect: CGRect, scale: CGFloat, traitCollection: UITraitCollection) -> UIImage {
        var image = UIImage()
        traitCollection.performAsCurrent {
            image = self.image(from: rect, scale: scale)
        }
        return image
    }
}

For dark mode, you can use:

let drawImage = drawing.thumbnail(rect: drawing.bounds, scale: 1.0, traitCollection: UITraitCollection(userInterfaceStyle: .dark))

For Light Mode:

let drawImage = drawing.thumbnail(rect: drawing.bounds, scale: 1.0, traitCollection: UITraitCollection(userInterfaceStyle: .light))
Rabel Ahmed
  • 1,131
  • 13
  • 12