Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS DrawRect code optimisation
    primarykey
    data
    text
    <p><img src="https://i.stack.imgur.com/7Y2uK.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/musYh.png" alt="enter image description here">Most of the time the code below works correctly and draws paths based upon the user's gestures. However, when the app initially launches the drawRect method isn't able to keep pace with the pan gesture, meaning that the pan gesture is sometimes called multiple times before drawrect is able to draw. This results in a space where the path should be drawn. How could I improve this please?</p> <pre><code>- (void)pan:(UIPanGestureRecognizer *)pan { if (pan.state == UIGestureRecognizerStateChanged) { inputPathCounter++; CGPoint velocity = [pan velocityInView:self]; previousPoint2 = previousPoint1; previousPoint1 = currentPoint; currentPoint = [pan locationInView:self]; float velocityMagnitude = sqrtf(velocity.x * velocity.x + velocity.y * velocity.y); float clampedVelocityMagnitude = clamp(VELOCITY_CLAMP_MIN, VELOCITY_CLAMP_MAX, velocityMagnitude); float normalizedVelocity = (clampedVelocityMagnitude - VELOCITY_CLAMP_MIN) / (VELOCITY_CLAMP_MAX - VELOCITY_CLAMP_MIN); float lowPassFilterAlpha = STROKE_WIDTH_SMOOTHING; float newThickness = (STROKE_WIDTH_MAX - STROKE_WIDTH_MIN) * normalizedVelocity + STROKE_WIDTH_MIN; self.lineWidth = self.lineWidth * lowPassFilterAlpha + newThickness * (1 - lowPassFilterAlpha); CGPoint mid1 = midPoint(previousPoint1, previousPoint2); CGPoint mid2 = midPoint(currentPoint, previousPoint1); CGMutablePathRef subpath = CGPathCreateMutable(); CGPathMoveToPoint(subpath, NULL, mid1.x, mid1.y); CGPathAddQuadCurveToPoint(subpath, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); CGRect drawBounds = CGPathGetBoundingBox(subpath); path = [[Path alloc] init]; path.width = self.lineWidth; [self selectedColor]; path.bezierPath = [UIBezierPath bezierPathWithCGPath:subpath]; path.num = inputPathCounter; CGPathRelease(subpath); CGRect drawBox = drawBounds; drawBox.origin.x -= self.lineWidth * 2.0; drawBox.origin.y -= self.lineWidth * 2.0; drawBox.size.width += self.lineWidth * 4.0; drawBox.size.height += self.lineWidth * 4.0; [self setNeedsDisplayInRect:drawBox]; } else if (pan.state == UIGestureRecognizerStateEnded | pan.state == UIGestureRecognizerStateCancelled) { [self saveImage]; self.lineWidth = STROKE_WIDTH_MIN; } } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); if(layer == nil) { //NSLog(@"reload layer"); layer = CGLayerCreateWithContext(context, bounds.size, NULL); layerContext = CGLayerGetContext(layer); CGContextScaleCTM(layerContext, scale, scale); viewRect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); //NSLog(@"viewrect %f %f",viewRect.size.width, viewRect.size.height); CGContextSaveGState(layerContext); UIGraphicsBeginImageContext (viewRect.size); CGContextTranslateCTM(layerContext, 0, self.image.size.height/scale); CGContextScaleCTM(layerContext, 1.0, -1.0); CGContextDrawImage(layerContext, viewRect, self.image.CGImage); UIGraphicsEndImageContext(); CGContextRestoreGState(layerContext); } UIBezierPath *bezierPath = path.bezierPath; CGContextAddPath(layerContext, bezierPath.CGPath); CGContextSetLineWidth(layerContext, path.width); CGContextSetStrokeColorWithColor(layerContext, path.color.CGColor); CGContextSetLineCap(layerContext, kCGLineCapRound); CGContextStrokePath(layerContext); CGContextDrawLayerInRect(context, viewRect, layer); } </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.
    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