首页 > 解决方案 > 如何快速在图像上绘制矩形

问题描述

您好,我正在尝试在 UIImageView 上绘制一些矩形,但是当我添加背景图像时,矩形绘图不再起作用:

这是代码:

import UIKit

class DrawView: UIView {

    var startPos: CGPoint?
    var endPos: CGPoint?
    
   
    override func draw(_ rect: CGRect) {
        
        guard let context = UIGraphicsGetCurrentContext() else {
            return
        }
        
        if startPos != nil && endPos != nil {
            print("both there")
            drawRectangle(context: context, startPoint: startPos!, endPoint: endPos!, isFilled: false)
        }
        
    }
    
    public func drawRectangle(context: CGContext, startPoint: CGPoint, endPoint: CGPoint, isFilled: Bool) {
        
        let xx = startPoint
        let xy = CGPoint(x:endPoint.x, y:startPoint.y)
        let yx = CGPoint(x:startPoint.x, y:endPoint.y)
        let yy = endPoint
        
        print("draw rectangle")
        
        context.move(to: xx)
        context.addLine(to: xx)
        context.addLine(to: xy)
        context.addLine(to: yy)
        context.addLine(to: yx)
        context.addLine(to: xx)
        
        context.setLineCap(.square)
        context.setLineWidth(8)
        
        if isFilled {
            context.setFillColor(UIColor.purple.cgColor)
            context.fillPath()
        } else {
            context.setStrokeColor(UIColor.red.cgColor)
            context.strokePath()
        }
        
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: self)
            print(position)
            startPos = position
        }
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: self)
            print(position)
            endPos = position
            setNeedsDisplay()
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: self)
            print(position)
            endPos = position
            
            setNeedsDisplay()
        }
        
    }
    
    
    func addBackground() {
        // screen width and height:
        let width = UIScreen.main.bounds.size.width
        let height = UIScreen.main.bounds.size.height

        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
        imageView.image = UIImage(named: "img1.jpg")

        // you can change the content mode:
        imageView.contentMode = UIView.ContentMode.scaleAspectFill

        self.addSubview(imageView)
        self.sendSubviewToBack(imageView)
    }

}

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var drawView: DrawView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        drawView.addBackground()
    }

    

}

我怀疑在设置图像后我没有使用正确的 UIGraphicsGetCurrentContext 但我无法弄清楚如何修复它。

标签: iosswiftuiimageview

解决方案


推荐阅读