【问题标题】:How to draw line on the UIImageView not for UIView?如何在 UIImageView 上画线而不是 UIView?
【发布时间】:2012-10-22 09:28:45
【问题描述】:

如何在 UIImageView 上画一条线?我正在使用 UIImageView 子类,它不支持drawRect: 方法。我想在图像视图上画一条线。我怎样才能做到这一点?请帮我。我使用下面的代码画线。

- (void)drawRect:(CGRect)rect {
    CGContextRef c = UIGraphicsGetCurrentContext();

    CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
    CGContextSetStrokeColor(c, red);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 5.0f, 5.0f);
    CGContextAddLineToPoint(c, 50.0f, 50.0f);
    CGContextStrokePath(c);
}

【问题讨论】:

  • 为什么要在图像视图上绘图?你也可以在 UIImage 上画线!!!
  • 我想裁剪图像,以便用户可以单击图像画线。

标签: iphone ios uiimageview


【解决方案1】:

以下代码的工作原理是创建一个与原始图像大小相同的新图像,将原始图像的副本绘制到新图像上,然后在新图像的顶部绘制一条 1 像素的线。

// UIImage *originalImage = <the image you want to add a line to>
// UIColor *lineColor = <the color of the line>

UIGraphicsBeginImageContext(originalImage.size);

// Pass 1: Draw the original image as the background
[originalImage drawAtPoint:CGPointMake(0,0)];

// Pass 2: Draw the line on top of original image
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, originalImage.size.width, 0);
CGContextSetStrokeColorWithColor(context, [lineColor CGColor]);
CGContextStrokePath(context);

// Create new image
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

// Tidy up
UIGraphicsEndImageContext();

【讨论】:

    【解决方案2】:

    首先,我不会使用UIImageView。事实上,文档说......

    如果您的子类需要自定义绘图代码,建议您使用 UIView 作为基类。

    使用UIView

    UIView 中添加一个UIImageView 子视图并将图像放入其中。现在您可以在UIViewdrawRect 方法中自定义绘图,它会出现在图像顶部。

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    
        CGContextSetLineWidth(context, 5.0);
    
        CGContextStrokeRect(context, self.bounds);
    }
    

    如果这不起作用,则可能不会调用 drawRect 方法。设置断点进行测试。

    【讨论】:

    • 这是 100% 完美的方式,但我已经尝试过这样的方式。但是这条线没有显示。所以只有我会转向另一个解决方案。请帮帮我。
    • 如果这条线没有显示,那么我建议调试绘图代码。确保它实际上正在运行并且它正在显示一些东西。 (也许先尝试不使用 UIImageView)。编辑显示一些我知道有效的绘制代码......
    • 我正在使用 UIView 类,然后添加 UIImageView 的子视图。终于用drawRect方法画图了……!但线没有显示。可能是图像会隐藏线..?请指导我。
    • 您不应该使用 drawRect 来绘制图像。让 UIImageView 完成它的工作。尝试删除 UIImageView。如果可行,那么您最好在 UIImageView 上添加一个 UIView 并绘制到其中。我将创建一个具有 UIImageView 和 UIView 子视图的全新自定义视图。第二个 UIView 是管理自定义绘图的子类。即 UIView 子类 - 子视图(其中带有 UIIMage 的 UIImageView,带有管理自定义绘图的 drawRect 的 UIView)。
    【解决方案3】:

    在我看来,最好的方法是采用与 UIImageView 大小相同的不可见 UIView 进行绘图,并在 UIView 上实现触摸方法进行绘图。

    你可以使用类似的东西:

    CGPoint temp=[touch locationInView:self];
    

    在以下方法中使用它:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    

    将所有点存储在一个数组中,然后将这些点原样绘制在 UIImageView 上。

    【讨论】:

      【解决方案4】:

      参考Muhammad Saad Ansari

      使用斯威夫特

      func drawLines ( originalImage:UIImage, lineColor:CGColor ) -> UIImage{
      
      UIGraphicsBeginImageContextWithOptions(originalImage.size,false,0.0)
      
      originalImage.drawInRect(CGRectMake( 0, 0, originalImage.size.width, originalImage.size.height ))
      
      let context = UIGraphicsGetCurrentContext();
      CGContextSetLineWidth(context, 5.0);
      CGContextMoveToPoint(context, 0, 0);
      CGContextAddLineToPoint(context, originalImage.size.width, 0);
      CGContextSetStrokeColorWithColor(context,lineColor);
      CGContextStrokePath(context);
      
      // Create new image
      var newImage = UIGraphicsGetImageFromCurrentImageContext();
      
      // Tidy up
      UIGraphicsEndImageContext();
      
      return newImage
      

      }

      【讨论】:

        猜你喜欢
        • 2015-01-30
        • 2014-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-21
        相关资源
        最近更新 更多