使用核心图形在UIImage上绘制矩形

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

enter image description here

这是我目前的代码:

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
}

我感觉它没有在我发送的图片上进行绘制,而是在创建一个全新的图片。整个图像都变成了红色,矩形是黑色的。我希望黑色矩形和其他部分仍然与原始图片相同。我做错了什么?

1
我想将图像绘制到上下文中可能是一个不错的开始 ;) - Hamish
3个回答

28

我感觉它并没有在我发送的图像上绘制,而是完全创建了一个新的图像。

没有涉及到感情。这就是这段代码的字面含义:

  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
}

9

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;
}

-2

你可以轻松实现它

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!)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接