How to draw a line on the UIImageView? I am using the UIImageView Sub Class, Its not support drawRect:
method. I want to draw a line on the image view. How can I do this? Please help me. I am using below code for draw line.
- (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);
}
Refer to Muhammad Saad Ansari
With Swift
}
First, I wouldn't use a
UIImageView
. In fact the docs say...Use a
UIView
.In the
UIView
add aUIImageView
subView and put the image in there. Now you can do you custom drawing in thedrawRect
method of theUIView
and it will appear on top of the image.If this isn't working then the drawRect method probably isn't being called. Set a breakpoint to test it.
The following code works by creating a new image the same size as the original, drawing a copy of the original image onto the new image, then drawing a 1 pixel line along to the top of the new image.
The best way to go about it in my opinion would be to take a invisible UIView of the same size as UIImageView for drawing and implement the touch methods on the UIView for drawing.
you can use something like:
Use this in methods like:
Store all the points in an array and then draw those points on the UIImageView as it is.