swift - 如何在uiimage视图上绘图
问题描述
我设法在 uiview 上绘图,我尝试使用相同的方法在 uiimage 视图上绘图,但根本没有用,请帮忙
guard let context = UIGraphicsGetCurrentContext() else { return }
lines.forEach { (line) in
context.setStrokeColor(line.color.cgColor)
context.setLineWidth(CGFloat(line.strokeWidth))
context.setLineCap(.round)
for (i, p) in line.points.enumerated() {
if i == 0 {
context.move(to: p)
} else {
context.addLine(to: p)
}
}
context.strokePath()
}
}
这是我的画布代码。我试图将 uiview 更改为 UIImageViews 但根本没有用。
class Canvas: UIView {
// public function
fileprivate var strokeColor = UIColor.black
fileprivate var strokeWidth: Float = 1
fileprivate var lines = [Line]()
绘制方法覆盖 func draw(_ rect: CGRect) { super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else { return }
lines.forEach { (line) in
context.setStrokeColor(line.color.cgColor)
context.setLineWidth(CGFloat(line.strokeWidth))
context.setLineCap(.round)
for (i, p) in line.points.enumerated() {
if i == 0 {
context.move(to: p)
} else {
context.addLine(to: p)
}
}
context.strokePath()
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
lines.append(Line.init(strokeWidth: strokeWidth, color: strokeColor, points: []))
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: nil) else { return }
guard var lastLine = lines.popLast() else { return }
lastLine.points.append(point)
lines.append(lastLine)
setNeedsDisplay()
}
}
解决方案
基于苹果文件:
不要将图像视图用于自定义绘图。UIImageView 类不使用 draw(_:) 方法绘制其内容。仅使用图像视图来呈现图像。要进行涉及图像的自定义绘图,请直接将 UIView 子类化并在那里绘制图像。
如果您的图像对于画布视图的背景是静态的,则可以将patternImage用于画布视图作为背景颜色。
从 ViewController 调用的代码示例:
// signatureView is a Canvas and set image as a background
if let img = UIImage(named: "bg.png") {
UIGraphicsBeginImageContext(signatureView.frame.size)
img.draw(in: signatureView.bounds)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let image = image {
signatureView.backgroundColor = UIColor(patternImage: image)
}
}
另一种解决方案是在画布类中使用 Image 属性,并在画布类的 Draw 方法中绘制图像,如下所示:
override func draw(_ rect: CGRect) {
super.draw(rect)
self.image?.draw(in: self.bounds)
guard let context = UIGraphicsGetCurrentContext() else { return }
现在您仍然可以使用 UIImageView 但 ImageView 应该是Canvas View 的子视图,在这种情况下(假设 UIImageView 已经有图像)调用如下:
// sign2ImageView as a sub view of Canvas
sign2ImageView.isUserInteractionEnabled = true
if let img = sign2ImageView.image { //or UIImage(named: "codinginswiftpanel")
sign2ImageView.image = nil
sign2ImageView.setNeedsDisplay()
signatureView2.image = img
}
这是输出:第一个是 UIView(Canvas) 的背景,第二个标记为橙色的是 UIImageView,它是修改后的 Canvas View 的子视图。
推荐阅读
- arrays - 如何使用 Dragula 的 drop 函数作为事件监听器来更新数组的顺序
- java - 防止在 httpClient 中发送保持活动状态
- javascript - 如何增加慢页面的灯塔超时
- python - 为什么'if'语句中的时间戳比较不能按照我的python程序中应用的逻辑工作?
- google-cloud-platform - 性能缓慢的 WebServer GoogleCloud
- r - How can I order the bars in my histogram?
- python - How to add attribute to class before __init__?
- python - Create a directory in /var/lib
- javascript - 使用保存和新建按钮时如何保留父值
- php - 如何使用 laravel-websockets 使 URL 可用于在公共频道中注册?