Note that there are some explanatory texts on larger screens.

plurals
  1. PODraw rectangle using Core Graphics with live preview
    primarykey
    data
    text
    <p>I'm creating a simple drawing application and would like some help with drawing rectangles based on the user's touch events. I'm able to draw a rectangle using Core Graphics from the point in touchesBegan to the point in touchesEnded. This rectangle is then displayed once the touchesEnded event is fired. </p> <p>However, I would like to be able to do this in a 'live' fashion. Meaning, the rectangle is updated and displayed as the user drags their finger. Is this possible? Any help would be greatly appreciated, thank you!</p> <p><strong>UPDATE:</strong></p> <p>I was able to get this to work by using the following code. It works perfectly fine for small images, however it get's very slow for large images. I realize my solution is hugely inefficient and was wondering if anyone could suggest a better solution. Thanks.</p> <pre><code> - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; _firstPoint = [touch locationInView:self.view]; _firstPoint.y -= 40; _lastPoint = _firstPoint; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.view]; currentPoint.y -= 40; [self drawSquareFrom:_firstPoint to:currentPoint]; _lastPoint = currentPoint; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.view]; currentPoint.y -= 40; [self drawSquareFrom:_firstPoint to:currentPoint]; [_modifiedImage release]; _modifiedImage = [[UIImage alloc] initWithCGImage:_imageView.image.CGImage]; } - (void)drawSquareFrom:(CGPoint)firstPoint to:(CGPoint)lastPoint { _imageView.image = _modifiedImage; CGFloat width = lastPoint.x - firstPoint.x; CGFloat height = lastPoint.y - firstPoint.y; _path = [UIBezierPath bezierPathWithRect:CGRectMake(firstPoint.x, firstPoint.y, width, height)]; UIGraphicsBeginImageContext( _originalImage.size ); [_imageView.image drawInRect:CGRectMake(0, 0, _originalImage.size.width, _originalImage.size.height)]; _path.lineWidth = 10; [[UIColor redColor] setStroke]; [[UIColor clearColor] setFill]; [_path fill]; [_path stroke]; _imageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload