On iOS 6.1, I have a view controller with a scrollView in which I need to display some images as the user flicks through the pages. The scrollView has 3 subviews to display the current, the previous and the next image respectively. Images are substituted during the scrolling. The mechanism works fine, but resources are not released as expected and the app is terminated after scrolling approximately 15 images.
At first I tried solving the problem simply assigning the new image to the UIImageView, but it did not work (I read that images are cached), so I tried a different approach:
// access the UIImageView of the currentView
for (UIView *subviewOfCurrentView in [currentView subviews]) {
if ([subviewOfCurrentView isKindOfClass:[UIImageView class]]) {
UIImageView *cv = (UIImageView *)subviewOfCurrentView;
//cv.image = nil;
[cv removeFromSuperview];
UIImageView *new_cv = [[UIImageView alloc] initWithFrame:photoRect];
new_cv.image = [UIImage imageNamed:[myObject getFileName];
[currentView addSubview:new_cv];
}
}
Profiling the app with the Activity Monitor shows that the Real Memory Usage keeps growing when I flick an image, despite the fact that the number of subviews remains constant.
Note that the app is terminated without calling didReceiveMemoryWarning.
How can I force my app to release the memory used by UIImage(s) and UIImageView(s)?
If you can help me, much appreciated.