Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 用核心图形在UIImage上绘制矩形_Ios_Swift_Uiimage_Core Graphics - Fatal编程技术网

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

Ios 用核心图形在UIImage上绘制矩形,ios,swift,uiimage,core-graphics,Ios,Swift,Uiimage,Core Graphics,我尝试使用Core Graphics在UIImage的中心放置一个矩形,大致如下所示: 这是我目前的代码: func drawRectangleOnImage(image: UIImage) -> UIImage { let imageSize = image.size let scale: CGFloat = 0 UIGraphicsBeginImageContextWithOptions(imageSize, false, scale) let con

我尝试使用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
}
我觉得它好像不是在我发送的图像上画图,而是在创造一个全新的图像。整个东西变成了红色,矩形变成了黑色。我想要黑色的矩形,它的其余部分应该仍然与图像相同

我做错了什么

我觉得它好像不是在我发过来的图片上面画的, 它创造了一个全新的形象

没有感情。这就是这段代码的作用

  • 创建一个新的上下文
  • 在其中绘制一个矩形
  • 从上下文中生成图像并返回它
  • 在步骤1和2之间,您需要将原始图像绘制到上下文中

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

    您还可以通过使用更高级的UIKITAPI来进一步简化绘图

    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
    }
    

    Kurt-Revis答案的目标C版本

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

    你很容易就能做到

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

    我认为将图像引入上下文将是一个良好的开端;)
    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!)