将 PKCanvasView 与子视图共享为图像



我有一个PKCanvasView,上面有一个UIImageView的子视图。

我想将视图分享为UIImage.这将是UIImageView,其上方的PKCanvasView.drawing

这是我获得绘图部分的方式。我只是不确定如何获得UIImageView

private func image(from canvas: PKCanvasView) -> UIImage {
let drawing = canvas.drawing
let visibleRect = canvas.bounds
let image = drawing.image(from: visibleRect, scale: UIScreen.main.scale)
return image
}

更新:

这是我目前正在尝试的:

let format = UIGraphicsImageRendererFormat.default()
format.opaque = false
let visibleRect = CGRect(x: 0, y: 0, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height)
let image = UIGraphicsImageRenderer(size: self.imageView.frame.size, format: format).image { context in
imageView.image?.draw(in: visibleRect, blendMode: .normal, alpha: 1.0)
canvasView.drawing.image(from: visibleRect, scale: UIScreen.main.scale).draw(at: .zero)
}

我面临的问题是我的图像从上到下被拉伸。有些图像比其他图像更多。

您需要创建一个新的图形上下文,将图像绘制到其中,然后在其上绘制绘图。您的代码应如下所示:

let format = UIGraphicsImageRendererFormat.default()
format.opaque = false
let image = UIGraphicsImageRenderer(size: imageView.frame.size, format: format).image { context in
imageView.image?.draw(at: .zero)
canvas.drawing.image(from: imageView.frame, scale: UIScreen.main.scale).draw(at: .zero)
}

相关内容

  • 没有找到相关文章

最新更新