如何使用CGContext在swift中程序性地绘制矩形/线条

时间:2014-08-10 15:10:00

标签: swift cgcontext rect

我一直在网上搜寻几天试图找到关于如何在Swift中以程序方式绘制矩形或线条的最简单的代码示例。我已经看过如何通过覆盖DrawRect命令来完成它。我相信你可以创建一个CGContext然后绘制成一个图像,但我很想看到一些简单的代码示例。或者这是一种可怕的方法?感谢。

class MenuController: UIViewController 
{

    override func viewDidLoad() 
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = UIColor.blackColor()

        var logoFrame = CGRectMake(0,0,118,40)
        var imageView = UIImageView(frame: logoFrame)
        imageView.image = UIImage(named:"Logo")
        self.view.addSubview(imageView)

        //need to draw a rectangle here
    }
}

3 个答案:

答案 0 :(得分:32)

这是一个创建自定义UIImage的示例,其中包含透明背景和红色矩形,其中的对角线穿过它。

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad();

        let imageSize = CGSize(width: 200, height: 200)
        let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 100, y: 100), size: imageSize))
        self.view.addSubview(imageView)
        let image = drawCustomImage(size: imageSize)
        imageView.image = image
    }
}

func drawCustomImage(size: CGSize) -> UIImage {
    // Setup our context
    let bounds = CGRect(origin: .zero, size: size)
    let opaque = false
    let scale: CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
    let context = UIGraphicsGetCurrentContext()!

    // Setup complete, do drawing here
    context.setStrokeColor(UIColor.red.cgColor)
    context.setLineWidth(2)

    context.stroke(bounds)

    context.beginPath()
    context.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
    context.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
    context.move(to: CGPoint(x: bounds.maxX, y: bounds.minY))
    context.addLine(to: CGPoint(x: bounds.minX, y: bounds.maxY))
    context.strokePath()

    // Drawing complete, retrieve the finished image and cleanup
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image!
}

答案 1 :(得分:9)

使用Swift 3.0的更新答案

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad();

        let imageSize = CGSize(width: 200, height: 200)
        let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 100, y: 100), size: imageSize))
        self.view.addSubview(imageView)
        let image = drawCustomImage(size: imageSize)
        imageView.image = image
    }
}

func drawCustomImage(size: CGSize) -> UIImage? {
    // Setup our context
    let bounds = CGRect(origin: CGPoint.zero, size: size)
    let opaque = false
    let scale: CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
    guard let context = UIGraphicsGetCurrentContext() else { return nil }

    // Setup complete, do drawing here
    context.setStrokeColor(UIColor.red.cgColor)
    context.setLineWidth(5.0)

    // Would draw a border around the rectangle
    // context.stroke(bounds)

    context.beginPath()
    context.move(to: CGPoint(x: bounds.maxX, y: bounds.minY))
    context.addLine(to: CGPoint(x: bounds.minX, y: bounds.maxY))
    context.strokePath()

    // Drawing complete, retrieve the finished image and cleanup
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

let imageSize = CGSize(width: 200, height: 200)
let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 100, y: 100), size: imageSize))
let image = drawCustomImage(size: imageSize)
imageView.image = image

答案 2 :(得分:3)

当其中一名球员获胜时,我使用了接受的答案在Tic Tac Toe比赛中画线。谢谢,很高兴知道它有效。不幸的是,我遇到了一些问题,让它同时在不同尺寸的iPhone和iPad上工作。这可能是应该解决的问题。基本上我所说的是,根据你的情况,它可能实际上不值得所有代码的麻烦。

我的替代解决方案是在Photoshop中简单地制作定制的,更好看的线条,然后使用UIImageView加载它。对我来说,这更简单,运行更好,看起来更好。显然,这实际上取决于你需要它。

步骤:

1:下载或创建图像(最好保存为.PNG)

2:将其拖入项目

3:将UIImage视图拖到故事板中

4:单击图像视图,然后在属性检查器中选择图像

5:按住Ctrl键并将图像视图拖动到.swift文件以声明出口

6:设置autolayout约束,使其适用于所有设备

在屏幕上和屏幕外动画,旋转和转换图像视图也可以说更容易

要更改图像:

yourImageViewOutletName.image = UIImage(named: "ImageNameHere")