I watched WWDC 2010 104 session about implementing tiling on UIScrollView and I tried to implement it with some changes,but i have problem with real memory – I add UIImageView as subview to UIScrollView and i even tried to remove super view immediately after adding – and i have not my real memory cleaned.At all.
May be there is a problem with autoreleasing?
- (void)tilePages
{
[scrollView setContentSize:CGSizeMake(scrollView.bounds.size.width * [images count], scrollView.bounds.size.height)];
CGRect visibleBounds = scrollView.bounds;
int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
int lastNeededPageIndex = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
lastNeededPageIndex = MIN(lastNeededPageIndex, ([images count] - 1));
for (ImageScrollView *page in visiblePages) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
[recycledPages addObject:page];
[page removeFromSuperview];
}
}
[visiblePages minusSet:recycledPages];
for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
if (![self isDisplayingPageForIndex:index]) {
ImageScrollView *page = [self dequeueRecycledPage];
if (page == nil) {
page = [[[ImageScrollView alloc] init] autorelease];
[page setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[page setContentMode:UIViewContentModeScaleAspectFill];
}
else {
[page setContentMode:UIViewContentModeScaleAspectFit];
}
}
[self configurePage:page forIndex:index];
[scrollView addSubview:page];
[visiblePages addObject:page];
}
}
}
- (ImageScrollView *)dequeueRecycledPage
{
ImageScrollView *page = [recycledPages anyObject];
if (page) {
[[page retain] autorelease];
[recycledPages removeObject:page];
}
return page;
}
- (BOOL)isDisplayingPageForIndex:(NSUInteger)index
{
BOOL foundPage = NO;
for (ImageScrollView *page in visiblePages) {
if (page.index == index) {
foundPage = YES;
break;
}
}
return foundPage;
}
- (void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index
{
page.index = index;
page.frame = [self frameForPageAtIndex:index];
[page displayImage:[self imageAtIndex:index]];
}
- (CGRect)frameForPagingScrollView
{
CGRect frame = scrollView.bounds;
return frame;
}
- (CGRect)frameForPageAtIndex:(NSUInteger)index {
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
CGRect pageFrame = pagingScrollViewFrame;
pageFrame.origin.x = (pagingScrollViewFrame.size.width * index);
return pageFrame;
}
- (UIImage *)imageAtIndex:(NSUInteger)index {
return [images objectAtIndex:index];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.scrollView = nil;
self.textView = nil;
}
- (void)dealloc
{
[super dealloc];
[scrollView release];
[images release];
[recycledPages release];
[visiblePages release];
}
You have implemented the demo correctly apart from one part. Memory is supposed to be released at the line
But i can see that you are storing your images in an array. Your images are being retained by that array and are not getting released, and as such are taking up memory.
The goal with this code is to lazily load the images as they are needed. You don’t post where you initialise the images array but from what you have it looks to be pre-populated with images.
Instead you should store the image paths in the array (everything you have done with it so far will still work) and then change the following method:
Or something to that effect. There may be more efficient ways of loading the image depending where the image is coming from but the above should stop the crashing.
If your images then take a long time to load and are blocking your interface you can do a couple of things. You can offload the image loading to a background thread using GCD or you can continue to watch that WWDC video and implement a
CATiledLayerfor the hi res image and place that over a low res image. The image will appear to crisp up at a very quick speed.