I have a scrollview that shows different images as it's scrolled through the pages, like PhotoScroller. I'm using ARC. When someone scrolls to another page, I set the image property of the UIImageView not being currently show to nil, as (attempting) to avoid memory crashes, which are still happening. Then when the user scrolls to a new page, the image for that page is set as the UIImageView's image property, as well as the page before and after it (for smooth viewing). The UIImage's for the pages are all held in an array. Yet as I scroll through the pages, memory usage keeps going up, as if setting the UIImageView's image
property to nil isn't releasing it from memory. I use initWithContentsOfFile
to initialize my UIImages. I tried with imageNamed
and imageWithContentsOfFile
too, with no luck. Here's my scrollview code:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int indexShown = self.scrollView.bounds.origin.x / kScrollObjWidth;
for(NSNumber *index in indexesToRemove)
{
UIImageView *imgViewToRemove = [[self.scrollView subviews] objectAtIndex:[index intValue]];
imgViewToRemove.image = nil;
}
[indexesToRemove removeAllObjects];
UIImageView *imgViewToReplace = [[self.scrollView subviews] objectAtIndex:indexShown];
[imgViewToReplace setImage:[pageUIImagesArr objectAtIndex:indexShown]];
[indexesToRemove addObject:[NSNumber numberWithInt:indexShown]];
if(indexShown != 0 && ![[[self.scrollView subviews] objectAtIndex:indexShown-1] image])
{
imgViewToReplace = [[self.scrollView subviews] objectAtIndex:indexShown-1];
[imgViewToReplace setImage:[pageUIImagesArr objectAtIndex:indexShown-1]];
[indexesToRemove addObject:[NSNumber numberWithInt:indexShown-1]];
}
if(indexShown != kNumImages-1 && ![[[self.scrollView subviews] objectAtIndex:indexShown+1] image])
{
imgViewToReplace = [[self.scrollView subviews] objectAtIndex:indexShown+1];
[imgViewToReplace setImage:[pageUIImagesArr objectAtIndex:indexShown+1]];
[indexesToRemove addObject:[NSNumber numberWithInt:indexShown+1]];
}
currentView = [[self.scrollView subviews] objectAtIndex:indexShown];
//check which view is being shown`
The UIImage's for the pages are all held in an array.
The UIImage
's are not being deallocated when you set the UIImageView
's property to nil
because the array is still holding a reference to them. As for the memory growth, it may be something else that is being allocated. I'd suggest taking a look with Instrument's object allocation instrument to track down what exactly is growing as you scroll.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With