Reputation: 3480
I'm developing an app that use a lot of images, I'm using the UIWebView
to represent about 200 image using JavaScript code (i'm using UIZE library), the problem is when i'm done with the UIWebView
, i'm using the following code in the viewWillDisappear
-(void)viewWillDisappear:(BOOL)animated {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[webViews stringByEvaluatingJavaScriptFromString:@"document.open();document.close();"];
}
with
- (void)viewDidUnload
{
webViews = nil;
}
- (void)dealloc {
[webViews release];
}
But, the reserved memory that taken from the UIWebView is still in memory,so my first question is How to force the app to free up the memory that got taken from the UIWebView
?
The same problem goes with UIImageView
, i'm using a large image (about 3072*2024) that will take (3072 * 2024 * 3 = 18 MB) from the memory, i'm loading about 8 image like that at once, so the reserved memory is huge, and when i'm trying to release them, the same thing happen, the reserved memory that taken from each image is still in the memory (I'm always get the warning that said Low Memory warning
). I've managed to load about 2 image of that size to reduce the reserved memory but the memory doesn't free up the memory until the low memory warning is appear, So, How can i reduce and deal with a huge number image and to free up the reserved memory at instance !?
Note:
I've used a hacked version for [UIImage imageNamed:];
method as the following:
@implementation UIImage(imageNamed_Hack)
+ (UIImage *)imageNamed:(NSString *)name {
return [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] bundlePath], name ] ];
}
@end
Any help will be appreciated.
Thx in advance.
Upvotes: 2
Views: 4245
Reputation: 1547
There are lot to do before you release and set to nil.
Remove all cached HTTP responses:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
Or even set the WebKit cache model:
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
Set the background to any color so that WebKit will clean up the cached display memory:
[webView stringByEvaluatingJavaScriptFromString:@"var body=document.getElementsByTagName('body')[0];body.style.backgroundColor=(body.style.backgroundColor=='')?'white':'';"];
Then close the document:
[webView stringByEvaluatingJavaScriptFromString:@"document.open();document.close()"];
Upvotes: 5
Reputation: 7466
Try this:
- (void)viewDidUnload
{
[webViews release];
webViews = nil;
}
It is really important to release before you set it to nil. Setting a variable to nil doesn't mean it is freed. You set its pointer to nil, but the memory where it pointed before will remain there and cause a leak. This is the main problem.
With the images, you can do the following:
Don't use a UIImageView in these cases, what I would propose, is to use CATiledLayer and render the images directly on it.
Here's the class reference for it: CATiledLayer
It has two sample applications. I think you should check the PhotoScroller application created by Apple, it can give you an idea how you can solve your problem. The main idea is to render the image in tiles, so only parts of the images will be rendered, that are on the screen currently, this saves a lot of memory, because it will not render off screen parts.
Upvotes: 4