当ScrollView太大时,它会耗尽内存 [英] ScrollView runs out of memory when it gets too big

查看:49
本文介绍了当ScrollView太大时,它会耗尽内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以为用户提供某种折线图的应用程序。我使用的是UIScrollView,其中包含带有图形的视图。该视图使用CoreGraphics以其drawrect方法绘制图形。
当图形过长时,就会出现问题。滚动浏览该图似乎很困难,最终该应用程序将耗尽内存并退出。环顾其他应用程序,我发现创建 WeightBot 应用程序的人能够管理长的连续图形而没有任何问题,所以显然我是用错误的方式做的。

I've an app which provides to the user some sort of a line graph. I'm using an UIScrollView which is containing the view with graph. The view is using CoreGraphics to draw the graph in it's drawrect method. The problem arises when the graph gets too long. Scrolling through the graph seems to stutter and eventually the app would run out of memory and exit. Looking around at other apps I see the guys who created the WeightBot app were able to manage long ongoing graphs without any problems so apparently I'm doing it the wrong way.

我想知道如何创建这种长线图而不会碰到内存问题?

I was wondering how this sort of long line graphs are created without bumping into memory issues?

编辑:添加一些代码

基本上,我所做的只是初始化在其中构建图形的视图drawRect方法并将视图作为子视图添加到scrollView。

Basically all I do is init the view which build's the graph in it's drawRect method and add the view as a subView to the scrollView.

这是实现视图的drawRect的方式:

This is how the view's drawRect is implemented:

- (void)drawRect:(CGRect)rect 
{
CGContextRef c = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(c, self.backgroundColor.CGColor);
CGContextFillRect(c, rect);

//... do some initialization 

 for (NSUInteger i = 0; i < xValuesCount; i++) 
    {
        NSUInteger x = (i * step) * stepX;

        NSUInteger index = i * step;

        CGPoint startPoint = CGPointMake(x + offsetX, offsetY);
        CGPoint endPoint = CGPointMake(x + offsetX, self.frame.size.height - offsetY);

        CGContextMoveToPoint(c, startPoint.x, startPoint.y);
        CGContextAddLineToPoint(c, endPoint.x, endPoint.y);
        CGContextClosePath(c);

        CGContextSetStrokeColorWithColor(c, self.gridXColor.CGColor);
        CGContextStrokePath(c);
    }
}


推荐答案

drawRect的 rect 自变量:指示要求您绘制视图的哪一部分。您应该添加一些逻辑来确定图形的哪些部分在该矩形中,并且仅绘制这些部分,而不是在每次调用时重新绘制整个内容。

The rect argument of drawRect: indicates which section of your view you're being asked to draw. You should add some logic to work out which parts of your graph are in that rect and only draw those, instead of redrawing the whole thing on every call.

这篇关于当ScrollView太大时,它会耗尽内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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