【发布时间】:2010-12-03 18:31:25
【问题描述】:
我正在使用 UIScrollView 实现图像浏览器。由于内存成本限制,我必须实现图像动态加载(我不想使用 CATiled 图层,因为它会强制用户等待加载每个图块)。
我尝试了多种方法:
- (UIImageView*) ldPageView{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool
NSError *error;
NSData *imData = [NSData dataWithContentsOfURL:ldPageRef options:NSDataReadingUncached error:&error];
UIImage *im = [[UIImage alloc] initWithData:imData];
ldView = [[UIImageView alloc] initWithImage:im] ;
[ldView setFrame:pageRect];
[pool release]; // Release the objects in the pool.
return ldView;
}
即使是这样
- (UIImageView*) ldPageView{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool
CGDataProviderRef provider = CGDataProviderCreateWithURL ((CFURLRef)ldPageRef);
CGImageRef d = CGImageCreateWithJPEGDataProvider(provider,nil, true,kCGRenderingIntentDefault);
UIImage *im = [[UIImage alloc] initWithCGImage:d];
ldView = [[[UIImageView alloc] initWithImage:im] autorelease];
[im release];
CGDataProviderRelease(provider);
CGImageRelease(d);
[ldView setFrame:pageRect];
[pool release]; // Release the objects in the pool.
return ldView;
}
但每次我在模拟器和 iPad 上尝试时,内存都会爆炸。我已经用 Instruments 运行了我的代码,并且没有报告泄漏。 ldView 是一个 istance 变量,它与对象 dealloc 上的 ldPageRef 一起被释放(肯定会调用它)。
我也尝试将 NSURLCache sharedCache 设置为零或零,但它仍在发生。
我已阅读内存管理指南,但一切对我来说似乎都还可以。 请帮忙
【问题讨论】: