PKDrawing different results for Dark Mode and Light Mode

大憨熊 提交于 2020-01-25 03:55:21

问题


When using PKDrawing in dark mode the image generated by imageFromRect:scale: returns pen drawings in white color. In light mode it uses black color. The background is usually transparent.

Question: How can PKDrawing be forced to always behave like in light mode?


回答1:


You can do it like this, by using 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
}


来源:https://stackoverflow.com/questions/58303655/pkdrawing-different-results-for-dark-mode-and-light-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!