我有一个具有多个 UIView 子类的应用程序,它们充当 UIScrollView 的页面。UIView 来回移动以向用户提供无缝体验。由于视图的内容绘制起来相当慢,它呈现在一个共享的 CGBitmapContext 上,由 NSOperation 子类的锁保护 - 在 NSOperationQueue 中一次执行 - 包装在 UIImage 中,然后由主线程用于更新内容的意见。
-(void)main {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
if([self isCancelled]) {
return;
}
if(nil == data) {
return;
}
// Buffer is the shared instance of a CG Bitmap Context wrapper class
// data is a dictionary
CGImageRef img = [buffer imageCreateWithData:data];
UIImage * image = [[UIImage alloc]initWithCGImage:img];
CGImageRelease(img);
if([self isCancelled]) {
[image release];
return;
}
NSDictionary * result = [[NSDictionary alloc]initWithObjectsAndKeys:image,@"image",id,@"id",nil];
// target is the instance of the UIView subclass that will use
// the image
[target performSelectorOnMainThread:@selector(updateContentWithData:) withObject:result waitUntilDone:NO];
[result release];
[image release];
[pool release];
}
UIView 子类的 updateContentWithData: 在主线程上执行也一样简单
-(void)updateContentWithData:(NSDictionary *)someData {
NSDictionary * data = [someData retain];
if([[data valueForKey:@"id"]isEqualToString:[self pendingRequestId]]) {
UIImage * image = [data valueForKey:@"image"];
[self setCurrentImage:image];
[self setNeedsDisplay];
}
// If the image has not been retained, it should be released together
// with the dictionary retaining it
[data release];
}
子类的 drawLayer:inContext: 方法只会从 UIImage 中获取 CGImage 并使用它来更新支持层或它的一部分。该过程不涉及保留或释放。
问题是一段时间后我的内存不足。UIView 的数量是静态的。CGImageRef 和 UIImage 被正确地创建、保留和释放(或者在我看来是这样)。Instruments 没有显示任何泄漏,只是可用内存不断下降,上升几次,然后下降甚至更低,直到应用程序终止。在此之前,该应用程序会循环浏览大约 2-300 个上述页面,但我希望在已经快速浏览了一堆页面之后,内存使用率会达到或多或少稳定的已用内存水平,或者,因为图像大小高达 3MB,较早耗尽。
任何建议将不胜感激。