【问题标题】:Converting PKDrawing to Image fails when dark mode is enabled启用暗模式时,将 PKDrawing 转换为图像失败
【发布时间】:2019-12-24 15:12:33
【问题描述】:

我正在尝试将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)
        }

第一张是关闭暗模式时的,第二张是开启时的

Reference 1 Reference 2

【问题讨论】:

    标签: swift


    【解决方案1】:

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

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-18
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      相关资源
      最近更新 更多