Sca*_*car 2 memory-management uiwebview uiimageview ios4
我正在开发一个使用大量图像的应用程序,我UIWebView使用JavaScript来代表大约200个图像使用JavaScript代码(我正在使用UIZE库),问题是当我完成了UIWebView,我正在使用以下代码中viewWillDisappear
-(void)viewWillDisappear:(BOOL)animated {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[webViews stringByEvaluatingJavaScriptFromString:@"document.open();document.close();"];
}
Run Code Online (Sandbox Code Playgroud)
同
- (void)viewDidUnload
{
webViews = nil;
}
- (void)dealloc {
[webViews release];
}
Run Code Online (Sandbox Code Playgroud)
但是,从UIWebView获取的保留内存仍然在内存中,所以我的第一个问题是如何强制应用程序释放从中获取的内存UIWebView?
同样的问题UIImageView,我正在使用一个大图像(约3072*2024)将从内存中采取(3072*2024*3 = 18 MB),我正在加载大约8个图像,所以保留的内存是巨大的,当我试图释放它们时,同样的事情发生了,从每个图像中取出的保留内存仍然在内存中(我总是得到警告说Low Memory warning).我已设法加载大约2个大小的图像以减少保留的内存,但内存不释放内存,直到出现低内存警告,所以,我怎样才能减少和处理大量的图像和在实例中释放保留的内存!?
注意:
我使用了一个黑客版本的[UIImage imageNamed:];方法,如下所示:
@implementation UIImage(imageNamed_Hack)
+ (UIImage *)imageNamed:(NSString *)name {
return [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] bundlePath], name ] ];
}
@end
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
Thx提前.
在发布之前有很多事可做,并设置为nil.
删除所有缓存的HTTP响应:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
Run Code Online (Sandbox Code Playgroud)
甚至设置WebKit缓存模型:
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
Run Code Online (Sandbox Code Playgroud)
将背景设置为任何颜色,以便WebKit将清理缓存的显示内存:
[webView stringByEvaluatingJavaScriptFromString:@"var body=document.getElementsByTagName('body')[0];body.style.backgroundColor=(body.style.backgroundColor=='')?'white':'';"];
Run Code Online (Sandbox Code Playgroud)
然后关闭文件:
[webView stringByEvaluatingJavaScriptFromString:@"document.open();document.close()"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4231 次 |
| 最近记录: |