Wednesday, 1 June 2016

UIImageView Draw Line

imageview.userInteractionEnabled = YES;
float brush = 1.0;
float red = 0.0;
float green = 0.0;
float blue = 0.0;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
   CGPoint  startingpoint = [touch locationInView:drawingImgvw];
}

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

    UITouch *touch = [touches anyObject];
    CGPoint pointNext = [touch locationInView:drawingImgvw];
    //UIGraphicsBeginImageContext(drawingImgvw.frame.size);
    UIGraphicsBeginImageContextWithOptions(drawingImgvw.frame.size, false, 0);
    [drawingImgvw.image drawInRect:CGRectMake(0, 0, drawingImgvw.frame.size.width, drawingImgvw.frame.size.height)];
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(),brush);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startingpoint.x, startingpoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), pointNext.x, pointNext.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawingImgvw.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    startingpoint = pointNext;

}

No comments:

Post a Comment