UIScrollView with many UIImageViews - Memory management
I have this huge problem with memory management.
The problem:
I've got aUIScrollView
, I've got an Array with 24 paths to Images in it and I want to show them in the UIScrollView
with paging enabled.
All images is in the size 1024x748 (iPad landscape resolution with status bar) and the filetype is jpg or png.
I'm using lazy loading just to not exceed the memory when the viewDidLoad
. And I'm going with the lazy load sample from Apple with the PageControl. Though I'm using UIImageViews
instead of UIViews
.
So my problem is that when I scroll to the third image, i want to remove the first image from the UIScrollView
and release its memory. Because the further I scroll, more memory is draining. When I page in the UIScrollView and a new image is loaded and added, about 5000kb more memory is used, and when i step in to the unloadPage:
(see below) nothing is released. Am I just "doing it wrong"?
How do I release and remove UIImageViews
properly?
(I'm loading the UIImages with initWithContentsOfFile:
)
Here is my code:
@interface SlideViewController : UIViewControllerExtended <UIScrollViewDelegate> {
ScrollViewController *slider;
IconView *currentChapter;
NSMutableArray *chapters;
NSMutableArray *views;
UIImageView *controller;
}
The lazy load function:
- (void) loadImageToScrollView:(NSInteger)chapter withPage:(NSInteger)page {
if (page < 0) return;
if (page >= chapterCount) return;
if([views objectAtIndex:page] != [NSNull null]) return;
NSMutableArr开发者_Go百科ay *all = [[currentChapter getImages] copy];
if(!([[all objectAtIndex:page] rangeOfString:@".mp4"].length > 0)) {
controller = [views objectAtIndex:page];
if((NSNull *)controller == [NSNull null]) {
NSArray *paths = [[all objectAtIndex:page] componentsSeparatedByString:@"."];
NSString *name = [[NSString alloc] initWithString:[paths objectAtIndex:0]];
NSString *ending = [[NSString alloc] initWithString:[paths objectAtIndex:1]];
NSString *file = [[NSString alloc] initWithFormat:@"%@", [[NSBundle mainBundle] pathForResource:name ofType:ending]];
UIImageView *tempImage = [[UIImageView alloc] initWithFrame:CGRectMake(page * 1024, 0, 1024, 768)];
UIImage *img = nil;
img = [[UIImage alloc] initWithContentsOfFile:file];
[tempImage setImage:img];
[tempImage setTag:page];
self.controller = tempImage;
[tempImage release];
[slider addSubview:controller];
[views replaceObjectAtIndex:page withObject:controller];
[name release];
[ending release];
[file release];
}
}
[all release];
}
The unload view method (which does not seem to work):
- (void) unloadPage: (int) page {
if(page < 0) return;
if(page >= chapterCount) return;
if((NSNull *)[views objectAtIndex:page] != [NSNull null]) {
UIImageView *viewToDelete = [views objectAtIndex:page];
[viewToDelete removeFromSuperview];
[views replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
The problem is solved by watching the #104
video session at WWDC2010. Which can be found on the Apple Developer site.
So if you have problem with memory leaks while loading big images, check it out. It's really, really useful.
VSScrollview though I am late but for other users, I have made a class, VSScrollview, which reuses its views like UITableView reuses its cell. Its use is simple. Go through the readme in the link
精彩评论