mdonati
mdonati

Reputation: 1099

Custom drawing on UIView gets pixelated when zooming in

I'm having some issues while trying to draw lines smoothly on touches moved when my UIView is zoomed in. The thing is that lines start to look very pixelated upon certain zoom level. My view hierarchy is really simple as of now, since this is more like a proof of concept. I have a UIScrollView, my UIView as a child and also it is set as the zoom view.

My drawRect: implementation looks like this:

CGContextRef context = UIGraphicsGetCurrentContext();

[self.layer renderInContext:context];

CGPoint mid1 = midPoint(_previousPoint1, _previousPoint2);
CGPoint mid2 = midPoint(_currentPoint, _previousPoint1);

CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, _previousPoint1.x, _previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextStrokePath(context);

Dumping the contents of the layer into the context is an important part since I'm only refreshing the bounding box enclosing the path formed by the last three touched points (_previousPoint1, _previousPoint2 and _currentPoint). That operation is actually done on touchesMoved method and the function that handles it is the following:

- (void)calculateMinImageArea:(CGPoint)pp1 :(CGPoint)pp2 :(CGPoint)cp
{
    // calculate mid point
    CGPoint mid1    = midPoint(pp1, pp2);
    CGPoint mid2    = midPoint(cp, pp1);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
    CGPathAddQuadCurveToPoint(path, NULL, pp1.x, pp1.y, mid2.x, mid2.y);
    CGRect bounds = CGPathGetBoundingBox(path);
    CGPathRelease(path);

    CGRect drawBox = [self extendedRect:bounds];

    UIGraphicsBeginImageContext(drawBox.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();

    [self setNeedsDisplayInRect:drawBox];
}

I was able to find a workaround using CAShapeLayers to draw the paths. However, the low performance wasn't acceptable.

I would really appreciate if someone can point me to the right approach.

Thanks.

Upvotes: 0

Views: 1293

Answers (1)

MadhuP
MadhuP

Reputation: 2019

[self setContentScaleFactor:scale];

use this in scrollViewDidEndZooming: delegate method

Hope this help's you

Upvotes: 1

Related Questions