是drawRect ----泄漏内存 [英] Is drawRect ---- leaking memory

查看:82
本文介绍了是drawRect ----泄漏内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在看到内存泄漏的问题,它似乎来自以下代码:

I am currenty seeing a problem with memory leaks, it seems to come from this code:

- (void)drawRect:(CGRect)rect
{

    CGImageRef cgImage = CGBitmapContextCreateImage(offScreenBuffer);
    UIImage *uiImage = [[UIImage alloc] initWithCGImage:cgImage];

    CGImageRelease(cgImage);

    [uiImage drawInRect:self.bounds];
    [uiImage release];

}

从触摸事件中调用此方法...

this method is called from touches events ...

-(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];

}

这真的很慢.... :(不知道为什么...

this is really slow....:( And I don't know why...

每次我在选定视图中绘制时,都会调用此方法,但是每次我需要创建图像并在其上显示时

Every single time I draw in the selected view, this is called, but every time I need to create an image and show it on the screen. Is there any possibility how to do this another way??

推荐答案

drawRect内发布的代码不应显示任何内容泄漏,因为图像已正确释放。

The posted code inside drawRect should not be showing any leaks, as the images are properly released.

这篇关于是drawRect ----泄漏内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆