Hello! I made a photo album viewer for iPhone. I have made a scrollview in which i put imageviews with the images. The images are pretty large, about 1 Mb. I have to show about 200 photos. I don't know why my solution leaks, I guess I don't release the imageviews well. I read many topics about these kinds of problems, about image caching and memory leaks when dealing with UIImage-s and UIImageView-s but none of them helped. My code is the following:
Adding ImageView-s to the scrollview and releasing the UIImageView-s if there are more then a defined number:
int currentPage = [page intValue];
UIImageView * imageView = [imageViewList objectAtIndex:currentPage];
if ((NSNull *)imageView == [NSNull null]) {
loadedImagesCount++;
NSString *url = [NSString stringWithFormat:@"%@", [imageUrls objectAtIndex:currentPage]];
[imageDatas replaceObjectAtIndex:currentPage withObject:[NSData dataWithContentsOfURL: [NSURL URLWithString: url]]];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[imageDatas objectAtIndex:currentPage]]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = NO;
[imageViewList replaceObjectAtIndex:currentPage withObject:imageView];
[imageView release];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * currentPage;
frame.origin.y = 0.0;
frame.size = CGSizeMake(frame.size.width, frame.size.height);
imageView.frame = frame;
[scrollView addSubview:imageView];
}
[[activityIndicators objectAtIndex:currentPage] stopAnimating];
if (loadedImagesCount >= MAX_IMAGE_COUNT) {
if (currentPage - MAX_IMAGE_COUNT >= 0) {
int remaining = currentPage - MAX_IMAGE_COUNT;
while ((remaining >= 0) && ((NSNull *)[imageViewList objectAtIndex:remaining] != [NSNull null])) {
((UIImageView *)[imageViewList objectAtIndex:remaining]).image = nil;
[[imageViewList objectAtIndex:remaining] removeFromSuperview];
[imageViewList removeObjectAtIndex:remaining];
[imageViewList insertObject:[NSNull null] atIndex:remaining];
[imageDatas removeObjectAtIndex:remaining];
[imageDatas insertObject:[NSNull null] atIndex:remaining];
loadedImagesCount--;
remaining--;
}
}
if (currentPage + MAX_IMAGE_COUNT < [imageViewList count]) {
int remaining = currentPage + MAX_IMAGE_COUNT;
while ((remaining < [imageViewList count]) && ((NSNull *)[imageViewList objectAtIndex:remaining] != [NSNull null])) {
((UIImageView *)[imageViewList objectAtIndex:remaining]).image = nil;
[[imageViewList objectAtIndex:remaining] removeFromSuperview];
[imageViewList removeObjectAtIndex:remaining];
[imageViewList insertObject:[NSNull null] atIndex:remaining];
[imageDatas removeObjectAtIndex:remaining];
[imageDatas insertObject:[NSNull null] atIndex:remaining];
loadedImagesCount--;
remaining++;
}
}
}
After I scroll about 40 photos the actual memory allocated is 14 Mb, of which cached memory is about 9 Mb. I don't know how to get rid of this cached memory, which is allocated through the CoreGraphics framework, zone_malloc method. Any suggestions are welcome. Thanks in advance. If you need more code please let me know. Bye