在暗模式下使用PKDrawing
时,imageFromRect:scale:生成的图像将以白色返回笔画。在灯光模式下,它使用黑色。背景通常是透明的。
问题:如何强制PKDrawing始终表现得像灯光模式?
答案 0 :(得分:3)
您可以使用traitCollection.performAsCurrent这样操作:
let darkImage = thumbnail(drawing: drawing, thumbnailRect: frameForImage, traitCollection: UITraitCollection(userInterfaceStyle: .dark))
func thumbnail(drawing: PKDrawing, thumbnailRect: CGRect, traitCollection: UITraitCollection) -> UIImage {
var image = UIImage()
traitCollection.performAsCurrent {
image = drawing.image(from: thumbnailRect, scale: 2.0)
}
return image
}