Iphone 如何在UIImageView上绘制线而不是UIView?

Iphone 如何在UIImageView上绘制线而不是UIView?,iphone,ios,uiimageview,Iphone,Ios,Uiimageview,如何在UIImageView上绘制线?我使用的是UIImageView子类,它不支持drawRect:方法。我想在图像视图上画一条线。我该怎么做?请帮帮我。我用下面的代码画线 - (void)drawRect:(CGRect)rect { CGContextRef c = UIGraphicsGetCurrentContext(); CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f}; CGContextSetStrokeColor(c,

如何在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);
}

首先,我不会使用
UIImageView
。事实上,医生说

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

使用
ui视图

UIView
中添加一个子视图
UIImageView
并将图像放入其中。现在,您可以在
UIView
drawRect
方法中进行自定义绘图,它将显示在图像的顶部

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

    CGContextSetLineWidth(context, 5.0);

    CGContextStrokeRect(context, self.bounds);
}

如果这不起作用,那么可能没有调用drawRect方法。设置一个断点来测试它。

在我看来,最好的方法是使用一个与用于绘图的UIImageView大小相同的不可见UIView,并在用于绘图的UIView上实现触摸方法

您可以使用以下内容:

CGPoint temp=[touch locationInView:self];
在以下方法中使用此选项:

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

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

以下代码的工作方式是创建一个与原始图像大小相同的新图像,将原始图像的副本绘制到新图像上,然后沿新图像顶部绘制一条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();
//UIImage*原始图像=
//UIColor*线条颜色=
UIGraphicsBeginImageContext(originalImage.size);
//第1步:绘制原始图像作为背景
[原始图像绘制点:CGPointMake(0,0)];
//第二步:在原始图像的顶部画一条线
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetLineWidth(上下文,1.0);
CGContextMoveToPoint(上下文,0,0);
CGContextAddLineToPoint(上下文,originalImage.size.width,0);
CGContextSetStrokeColorWithColor(上下文,[lineColor CGColor]);
CGContextStrokePath(上下文);
//创建新图像
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
//收拾
UIGraphicsSendImageContext();
请参阅

迅速地

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

}

为什么要在图像视图上绘图?你也可以在UIImage上画线!!!我想裁剪图像,这样用户可以点击图像来画线。这是100%完美的方式,但我已经这样尝试了。但是这条线没有显示出来。因此,只有我会转移到另一个解决方案。请帮帮我。如果线条没有显示,我建议调试绘图代码。确保它实际上正在运行,并且显示了一些内容。(可以先尝试不使用UIImageView)。编辑以显示一些我知道有效的绘图代码…我正在使用UIView类,然后添加UIImageView的子视图。最后使用drawRect方法绘制图像。。。!但这条线并没有显示出来。可能是图像将隐藏线。。?请引导我。你不应该使用drawRect来绘制图像。让UIImageView完成它的工作。尝试删除UIImageView。如果这样做有效,那么最好在UIImageView上添加一个UIView,并在其中绘制图形。我将创建一个全新的自定义视图,其中包含UIImageView和UIView子视图。第二个UIView是管理自定义图形的子类。i、 e.UIView子类-子视图(包含UIIMage的UIImageView,包含管理自定义图形的drawRect的UIView)。