Dim*_*ris 11 iphone performance drawing objective-c uiview
据我所知,到目前为止,每次我在drawRect:UIView中绘制一些内容时,整个上下文都会被删除然后重新绘制.
所以我必须做这样的事情来绘制一系列点:
方法A:在每次通话时绘制所有内容
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, self.bounds, maskRef); //draw the mask
CGContextClipToMask(context, self.bounds, maskRef); //respect alpha mask
CGContextSetBlendMode(context, kCGBlendModeColorBurn); //set blending mode
for (Drop *drop in myPoints) {
CGContextAddEllipseInRect(context, CGRectMake(drop.point.x - drop.size/2, drop.point.y - drop.size/2, drop.size, drop.size));
}
CGContextSetRGBFillColor(context, 0.5, 0.0, 0.0, 0.8);
CGContextFillPath(context);
}
Run Code Online (Sandbox Code Playgroud)
这意味着,每次我想添加一个新的时候,我必须存储我所有的Dots(没关系),然后逐个重新绘制它们.不幸的是,这给了我糟糕的表现,我相信还有其他方法可以做到这一点,效率更高.
编辑:使用MrMage的代码,我做了以下,不幸的是,同样慢,颜色混合不起作用.我可以尝试其他任何方法吗?
方法B:保存UIImage中的先前绘制并仅绘制新内容和此图像
- (void)drawRect:(CGRect)rect
{
//draw on top of the previous stuff
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext(); // ctx is now the image's context
[cachedImage drawAtPoint:CGPointZero];
if ([myPoints count] > 0)
{
Drop *drop = [myPoints objectAtIndex:[myPoints count]-1];
CGContextClipToMask(ctx, self.bounds, maskRef); //respect alpha mask
CGContextAddEllipseInRect(ctx, CGRectMake(drop.point.x - drop.dropSize/2, drop.point.y - drop.dropSize/2, drop.dropSize, drop.dropSize));
CGContextSetRGBFillColor(ctx, 0.5, 0.0, 0.0, 1.0);
CGContextFillPath(ctx);
}
[cachedImage release];
cachedImage = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
//draw on the current context
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, self.bounds, maskRef); //draw the mask
CGContextSetBlendMode(context, kCGBlendModeColorBurn); //set blending mode
[cachedImage drawAtPoint:CGPointZero]; //draw the cached image
}
Run Code Online (Sandbox Code Playgroud)
编辑:毕竟我结合下面提到的方法之一与仅在新的rect重绘.结果是: 快速方法:
- (void)addDotAt:(CGPoint)point
{
if ([myPoints count] < kMaxPoints) {
Drop *drop = [[[Drop alloc] init] autorelease];
drop.point = point;
[myPoints addObject:drop];
[self setNeedsDisplayInRect:CGRectMake(drop.point.x - drop.dropSize/2, drop.point.y - drop.dropSize/2, drop.dropSize, drop.dropSize)]; //redraw
}
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, self.bounds, maskRef); //draw the mask
CGContextClipToMask(context, self.bounds, maskRef); //respect alpha mask
CGContextSetBlendMode(context, kCGBlendModeColorBurn); //set blending mode
if ([myPoints count] > 0)
{
Drop *drop = [myPoints objectAtIndex:[myPoints count]-1];
CGPathAddEllipseInRect (dotsPath, NULL, CGRectMake(drop.point.x - drop.dropSize/2, drop.point.y - drop.dropSize/2, drop.dropSize, drop.dropSize));
}
CGContextAddPath(context, dotsPath);
CGContextSetRGBFillColor(context, 0.5, 0.0, 0.0, 1.0);
CGContextFillPath(context);
}
Run Code Online (Sandbox Code Playgroud)
感谢大家!
如果您每次绘制时实际上只更改了UIView内容的一小部分(并且其余内容通常保持不变),则可以使用此方法.不是每次都重绘UIView的所有内容,而是只标记需要重绘的视图区域-[UIView setNeedsDisplayInRect:]而不是-[UIView setNeedsDisplay].您还需要确保在绘图之前未通过设置清除图形内容view.clearsContextBeforeDrawing = YES;
当然,所有这些也意味着你的drawRect:实现需要尊重rect参数,这应该是整个视图的rect的一小部分(除非其他东西弄脏了整个矩形),并且只绘制那个部分.