【发布时间】:2012-01-19 12:56:49
【问题描述】:
我目前看到内存泄漏的问题,它似乎来自以下代码:
- (void)drawRect:(CGRect)rect
{
CGImageRef cgImage = CGBitmapContextCreateImage(offScreenBuffer);
UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];
CGImageRelease(cgImage);
[uiImage drawInRect:self.bounds];
[uiImage release];
}
这个方法是从触摸事件中调用的……
-(void)drawPoint:(UITouch *)touch {
currentLoc = [[PointLocation alloc] init];
currentLoc.location = [touch locationInView:self];
self.previousPoint = self.point;
self.point = currentLoc;
[self drawToBuffer];
[currentLoc release];
}
这是绘制到缓冲区....
-(void)drawToBuffer {
CGFloat color[4] = {R,G,B,A};
if (self.previousPoint != nil) {
CGContextSetRGBStrokeColor(offScreenBuffer, color[0],color[1],color[2],color[3]);
CGContextBeginPath(offScreenBuffer);
CGContextSetLineWidth(offScreenBuffer, lane);
CGContextSetLineCap(offScreenBuffer, kCGLineCapRound);
CGContextMoveToPoint(offScreenBuffer, previousPoint.location.x, previousPoint.location.y);
CGContextAddLineToPoint(offScreenBuffer, point.location.x, point.location.y);
CGContextDrawPath(offScreenBuffer, kCGPathStroke);
}
[self setNeedsDisplay];
}
这真的很慢......:(我不知道为什么......
每次我在选定的视图中绘制时,都会调用它,但每次我需要创建一个图像并将其显示在屏幕上。有没有可能以另一种方式做到这一点??
【问题讨论】:
-
这里没有泄漏,因为您释放了所有内容。你有另一个问题,在另一个地方。
-
"我目前看到内存泄漏问题";什么工具向您显示这些泄漏?用于查找内存泄漏的第一个工具是分析器。 (在Xcode菜单栏Product->Analyze)
-
当然,这些工具没有显示泄漏,但在这段代码中,我正在移动并传递整个图像.....我确定有问题......:(但我需要这段代码显示了绘制的内容...
-
但我的问题是是否有可能以不同的方式做同样的事情???
-
至少有一种稍微不同的方法可以在 drawRect 中绘制位图,至少还有两种方法可以在使用 drawRect 之外显示位图。但这不是你的问题标题。
标签: ios uiview memory-leaks uiimage drawrect