启用暗模式时,将PKDrawing转换为图像失败

时间:2019-11-17 23:37:59

标签: swift

我正在尝试将PKDrawing转换为UIImage并将其保存到照片中。当关闭暗模式时,此功能可以正常工作,但是当启用暗模式时,由于保存为图像时背景颜色为白色,因此白线消失了。

以下摘要:self.traitCollection.performAsCurrent不能解决问题

这是我使用下面的参考所尝试的代码(结果图像在下面)

1

self.traitCollection.performAsCurrent {
var image:UIImage!
self.view.traitCollection.performAsCurrent {
    let drawing = note.drawing as? PKDrawing
    image = drawing!.image(from: drawing!.bounds, scale: 1.0)
}

let action = UIActivityViewController(activityItems: [image!],
                                      applicationActivities: nil)
action.popoverPresentationController?.sourceView = self.collectionView.cellForItem(at: indexPath)
self.present(action, animated: true, completion:nil)
}

2

self.traitCollection.performAsCurrent {
            var image:UIImage!
            var drawing:PKDrawing!
            drawing = note.drawing as? PKDrawing
            image = drawing.image(from: drawing.bounds, scale: 1.0)
            UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
        }

第一张图片来自黑暗模式关闭时的图像,第二张图片来自开启黑暗模式

Dark Mode Off Image Dark Mode Image

Reference 1 Reference 2

1 个答案:

答案 0 :(得分:0)

这不是完美的答案,但对于我的情况,我通过在代码中强制关闭暗模式来解决了

var image:UIImage!
let userInterfaceStyle = self.traitCollection.userInterfaceStyle                   
self.overrideUserInterfaceStyle = .light
let drawing = note.drawing as? PKDrawing
self.overrideUserInterfaceStyle = .unspecified

希望这会有所帮助