【发布时间】:2011-08-28 10:36:58
【问题描述】:
我将一些图形缓存到 CGLayers 上,然后使用 @encode 将它们存储在 NSValue 对象中(以便将它们存储在数组中)。我只是想确保我正确处理保留/释放...
我缓存图形并将它们存储在数组中,如下所示:
// Create an NSMutableArray "newCache"
CGLayerRef drawingLayer = CGLayerCreateWithContext(context, bounds.size, NULL);
CGContextRef drawingContext = CGLayerGetContext(drawingLayer);
// Do some drawing...
[newCache addObject:[NSValue valueWithBytes:&drawingLayer objCType:@encode(CGLayerRef)]];
CGLayerRelease(drawingLayer); // Is this release correct?
然后我检索图层:
CGLayerRef retrievedLayer;
[(NSValue *)[cacheArray objectAtIndex:index] getValue:&retrievedLayer];
// Use the layer...
// Should I release retrievedLayer here?
我是否认为该层在添加到数组后需要释放(第一个代码 sn-p 中的最后一行)?我假设是这种情况,因为我在代码的前面调用了一个 create 函数。然后 NSValue 会为我跟踪图层数据吗?检索到的Layer在使用后需要手动释放吗?
谢谢
【问题讨论】:
-
您使用数组有什么原因吗?当内存不足时,您可能会发现 NSCache 更易于使用且更有帮助。
-
公平一点 - 不,我只使用一个数组,因为有足够的层需要一个集合。我以前没有真正使用过 NSCache,但这看起来可能是它的理想选择。
标签: ios memory-management core-graphics release-management cglayer