iPhone + UIView。绘制矩形期间消耗大量内存。有什么策略可以减少这种情况吗?

标签 iphone uiview drawrect

我的数据可视化应用程序在重绘期间会出现大量内存消耗高峰(setNeedsDisplay 会触发drawRect)。我目前正在重新绘制包含数据图的整个 View 。该 View 比设备显示屏大得多。

有没有办法告诉 CoreGraphics 分配足够的内存来绘制每个元素(每个元素是一个比设备显示小得多的小矩形 block )并在完成后释放内存,而不是我当前的天真的方法?

提前致谢。

-道格

美国东部时间 12 月 8 日上午 8:28 更新

这是带有解释性文字的相关代码。我正在运行带有 ObjectAlloc、内存监视器和泄漏仪器的仪器。我唯一的内存泄漏是由于 NSOperationQueue 不释放 mems 造成的。这是次要的且不相关的。

从架构上讲,该应用程序由一个 tableView 组成,其中包含人类基因组中要检查的有趣位置的列表。当选择表行时,我将返回称为alignmentData 的数据的数据收集操作排入队列。然后将该数据绘制为水平矩形板。

最初,当 tableView 启动时,我的内存占用量为 5 MB。

- (void)viewWillAppear:(BOOL)animated {

    // Initial dimensions for the alignment view are set here. These
    // dimensions were roughed out in IB.

    frame                       = self.alignmentView.frame;
    frame.origin.x              = 0.0;
    frame.origin.y              = 0.0;  
    frame.size.width            = self.scrollView.contentSize.width;    
    frame.size.height           = 2.0 * (self.containerView.frame.size.height);

}

注意:调用viewWillAppear:后内存占用并没有变化。即使alignmentView的大小远远超出了显示器的尺寸。

这是从数据收集操作中调用的方法。

- (void)didFinishRetrievingAlignmentData:(NSDictionary *)results {

        // Data retrieved from the data server via the data gathering operation
    NSMutableData *alignmentData = [[results objectForKey:@"alignmentData"] retain];

    NSMutableArray *alignments = [[NSMutableArray alloc] init];
    while (offset < [alignmentData length]) {

        // ...
        // Ingest alignmentData in alignments array 
        // ...  

    } // while (offset < [alignmentData length])    
    [alignmentData release];

    // Take the array of alignment objects and position them in screen space 
        // so that they pack densely creating horizontal rows of alignment objects 
        // in the process.
    self.alignmentView.packedAlignmentRows = 
    [Alignment packAlignments:alignments basepairStart:self.startBasepairValue basepairEnd:self.endBasepairValue];  
    [alignments release];

    [self.alignmentView setNeedsDisplay];

}

在这行代码之后:

self.alignmentView.packedAlignmentRows = ...

内存占用为 13.8 MB

在这行代码之后:

[self.alignmentView setNeedsDisplay];

内存占用量激增至 21.5 MB,并停留几秒钟,然后返回到先前存在的 13.8 MB 水平

我正在寻找的解决方案本质上允许我创建一个水平渲染缓冲区窗口,它是单行对齐对象的高度。我会将其内存渲染分配给它,然后丢弃它。我会对每一行对齐数据一遍又一遍地执行此操作。

理论上,我可以用这种方法渲染无限量的数据,这当然是最好的;-)。

-道格

最佳答案

这是对我的内存问题的不太明显的答案。我会给自己这个,因为我是从 Rincewind 的 Apple 开发论坛上学到的,Rincewind 是一位非常乐于助人的 Apple 工程师。

事实证明,通过将大 View 分割成 N 个小块并依次渲染每个小块,我将产生大约是大 View 大小的 1/N 的内存峰值。

因此,对于每个较小的 View :alloc/init,提供我的一部分数据,setNeedsDisplay。对所有 N 个小 View 进行冲洗/重复。

很简单吧?

在学习这一点之前,我错误地认为 setNeedsDisplay:myRect 是为大 View 执行此操作的。显然不是。

感谢大家的建议。

干杯, 道格 @杜格拉

关于iPhone + UIView。绘制矩形期间消耗大量内存。有什么策略可以减少这种情况吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1864218/

相关文章:

iphone - 在 iPhone 应用程序中使用 PayPal API

iphone - textFieldDidBeginEditing - 不工作

iPhone 在应用程序启动时出现黑屏

ios - UIView是隐藏函数, "the view' s next valid key view”是什么意思?

ios - 如何使用 CGContextRef 绘制两个单独的形状

objective-c - 在更快的设备上绘制矩形的奇怪行为

iphone - NSData 到 UIImage

iOS + MKMapView 用户触摸绘图

ios - 无法通过编程约束在 UIScrollView 中看到 subview

objective-c - 在 iPhone 上用手指画直线