Disadvantages to using CGMutablePathRef?

百般思念 提交于 2019-12-24 11:07:15

问题


I have this code to draw rounded rectangles:

void ContextAddRoundedRect(CGContextRef ctx, CGRect rect, CGFloat radius) {
    CGFloat minX = CGRectGetMinX(rect);
    CGFloat maxX = CGRectGetMaxX(rect);
    CGFloat minY = CGRectGetMinY(rect);
    CGFloat maxY = CGRectGetMaxY(rect);

    CGContextMoveToPoint(ctx, minX + radius, minY);
    CGContextAddArcToPoint(ctx, maxX, minY, maxX, minY + radius, radius);
    CGContextAddArcToPoint(ctx, maxX, maxY, maxX - radius, maxY, radius);
    CGContextAddArcToPoint(ctx, minX, maxY, minX, maxY - radius, radius);
    CGContextAddArcToPoint(ctx, minX, minY, minX + radius, minY, radius);
}

I'm going to convert it to use a CGMutablePathRef for the drawing, because then I can use -[UIBezierPath containsPoint:] (by using +bezierPathWithCGPath: first, as specified in the comments and answer here.)

Are there are any serious/known disadvantages to using CGMutablePathRef as opposed to just drawing directly on the CGContext? My guess is that it's a non-starter, but maybe there's something I'm missing.

(I know I have to release it)


回答1:


seems an ok approach to me... maybe you can even cache it somehow? *I mean don't redo the path every time but only when rect or radius change

of course a mutable path will have a higher memory footprint but that should be ignorable in this case


e.g.:

UIBezierPath *pathWithRoundedRect(NSRect rect, CGFloat radius) {
    CGMutablePathRef path;
    ...

    id p = [UIBezierPath bezierPathWithCGPath:path];
    CGPathRelease(path);
    return p;

}




回答2:


Just use +[UIBezierPath bezierPathWithRoundedRect:cornerRadius:] and save yourself all the trouble. If you really need a CGPathRef from it, then get the CGPath property.

Unless you do this thousands of times per second, it's unlikely to be noticeably slow or take a significant amount of memory.




回答3:


No, CGContext has an internal path, the functions on CGContext to draw, are just calling the equivalent functions on its internal CGMutablePath. There's no reason to not make the CGMutablePath yourself, the path functions on CGContext are a convenience.

If you're not going to draw the path using the CGContext then you're better to make a CGMutablePath.



来源:https://stackoverflow.com/questions/14299140/disadvantages-to-using-cgmutablepathref

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!