【问题标题】:Draw Rectangle On UIImage with Core Graphics使用 Core Graphics 在 UIImage 上绘制矩形
【发布时间】:2016-05-21 21:13:28
【问题描述】:

我正在尝试使用 Core Graphics 在 UIImage 的中心放置一个矩形,大致如下所示:

这是我目前的代码:

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
}

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

我做错了什么?

【问题讨论】:

  • 我想将图像绘制到上下文中会是一个好的开始;)

标签: ios swift uiimage core-graphics


【解决方案1】:

我觉得好像它不是在我发送的图像之上绘制的, 它正在完全创建一个新图像。

不涉及感情。这实际上就是这段代码的作用。

  1. 创建一个新的上下文
  2. 在其中绘制一个矩形
  3. 从上下文中生成图像,然后返回它

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

此外,无需设置线宽或描边颜色,因为您只是填充矩形,而不是描边。 (如果您因为某种原因看到红色,那不是因为这段代码;您是否有其他具有红色背景的视图或图像?)

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 来进一步简化绘图。

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
}

【讨论】:

    【解决方案2】:

    Objective C 版本的 Kurt Revis 答案

    -(UIImage *)drawRectangleOnImage:(UIImage *)img rect:(CGRect )rect{
          CGSize imgSize = img.size;
          CGFloat scale = 0;
          UIGraphicsBeginImageContextWithOptions(imgSize, NO, scale);
          [img drawAtPoint:CGPointZero];
          [[UIColor greenColor] setFill];
          UIRectFill(rect);
          UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          return newImage;
    }
    

    【讨论】:

      【解决方案3】:

      你可以轻松实现它

      guard let img = actualImage else { return }
      let croppedImage = img.cgImage!.cropping(to: CGRect(x: 0, y: 0, width: 50, height: 20))
      let img = UIImage(cgImage: croppedImage!)
      

      【讨论】: