Setting uiimage to nil does not free memory using ARC

I have a scrollview that shows different images when scrolling pages, like PhotoScroller. I am using ARC. When someone scrolls to another page, I set the image property of UIImageView, which is not currently displayed in nil, as (try) to avoid memory failures that still occur. Then, when the user scrolls to a new page, the image for this page is set as a UIImageView image property, as well as the page before and after it (for smooth viewing). UIImage for pages are stored in an array. However, as I browse the pages, memory usage continues to increase, as if setting the UIImageView image property to nil does not free it from memory. I use initWithContentsOfFile to initialize my UIImages. I tried with imageNamed and imageWithContentsOfFile too, no luck. Here is my scroll 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` 
+4
source share
1 answer

UIImage for pages are stored in an array.

UIImage not freed when you set the UIImageView property to nil because the array still holds a reference to them. As for memory growth, it could be something else that stands out. I would advise taking a look at the instrument's object distribution tool to find out what exactly grows as you scroll.

+1
source

Source: https://habr.com/ru/post/1379260/


All Articles