使用核心图形在UIImage上绘制矩形 [英] Draw Rectangle On UIImage with Core Graphics

查看:178
本文介绍了使用核心图形在UIImage上绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Core Graphics在UIImage的中心放置一个矩形,大致看起来像这样:

I'm trying to put a rectangle at the center of a UIImage using Core Graphics to look roughly like this:

到目前为止,这是我的代码:

This is my code so far:

func drawRectangleOnImage(image: UIImage) -> UIImage {
    let imageSize = image.size
    let scale: CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(imageSize, false, scale)
    let context = UIGraphicsGetCurrentContext()

    let rectangle = CGRect(x: 0, y: (imageSize.height/2) - 30, width: imageSize.width, height: 60)

    CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor)
    CGContextSetStrokeColorWithColor(context, UIColor.redColor().CGColor)
    CGContextSetLineWidth(context, 5)
    CGContextAddRect(context, rectangle)
    CGContextDrawPath(context, .Fill)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

我感觉好像它不在我发送的图像上,而是在完全创建一个新图像。整个东西变成红色,矩形变成黑色。我想要黑色矩形,其余的矩形仍应与图像相同。

I feel as if it's not drawing on top of the image which I sent in, it's creating a new image altogether. The entire thing is becoming red, with the rectangle black. I want the black rectangle and the rest of it should still be the same as the image.

我在做什么错了?

推荐答案


我感觉好像它不在我发送的图像上,
完全在创建一个新图像。

I feel as if it's not drawing on top of the image which I sent in, it's creating a new image altogether.

没有感觉。


  1. 创建新上下文

  2. 在其中绘制矩形

  3. 使图像脱离上下文,并返回图像

在步骤1和2之间您需要将原始图像绘制到上下文中。

Between steps 1 and 2, you need to draw your original image into the context.

此外,由于您仅填充矩形而不是填充矩形,因此无需设置线宽或笔触颜色抚摸它。 (如果您由于某种原因看到红色,那不是因为此代码;您是否有一个具有红色背景的其他视图或图像?)

Also, there is no need to set the line width or stroke color, since you are only filling the rectangle, not stroking it. (If you are seeing red for some reason, it wasn't because of this code; do you have a different view or image that has a red background?)

func drawRectangleOnImage(image: UIImage) -> UIImage {
    let imageSize = image.size
    let scale: CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(imageSize, false, scale)
    let context = UIGraphicsGetCurrentContext()

    image.draw(at: CGPoint.zero)

    let rectangle = CGRect(x: 0, y: (imageSize.height/2) - 30, width: imageSize.width, height: 60)

    CGContextSetFillColorWithColor(context, UIColor.black.CGColor)
    CGContextAddRect(context, rectangle)
    CGContextDrawPath(context, .Fill)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

并且您可以通过使用更高级别的UIKit API进行绘制来进一步简化。

And you could further simplify by using higher-level UIKit API to draw.

func drawRectangleOnImage(image: UIImage) -> UIImage {
    let imageSize = image.size
    let scale: CGFloat = 0
    UIGraphicsBeginImageContextWithOptions(imageSize, false, scale)

    image.draw(at: CGPoint.zero)

    let rectangle = CGRect(x: 0, y: (imageSize.height/2) - 30, width: imageSize.width, height: 60)

    UIColor.black.setFill()
    UIRectFill(rectangle)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

这篇关于使用核心图形在UIImage上绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆