首页 > 解决方案 > 如何在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()
}

}

标签: swift

解决方案


基于苹果文件

不要将图像视图用于自定义绘图。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 的子视图。

在此处输入图像描述


推荐阅读