Low memory warning with several EGOImageView
-
20-06-2021 - |
Question
I ask for your help with a memory problem in my app!
I've got a cell in a table which contains a horizontal scrollview with one or more EGOImageView
s. This cell is always located in the row 0, but I often have to reload this cell with another Image.
The problem is: when I reload my cell too many times (30 or 40 times), I receive a memory warning or sometimes no errors and the app crashes...
I use ARC mod. Here is a preview of my code:
Cell code
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier animated:(BOOL)animated{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self.contentView setFrame:CGRectMake(0, 0, 320, 240)];
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 240)];
[self.scrollView setDelegate:self];
[self.scrollView setPagingEnabled:YES];
[self.scrollView setShowsHorizontalScrollIndicator:NO];
self.scrollView.contentSize = CGSizeMake(320, 240);
pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 220, 320, 20)];
[pageControl setCurrentPage:0];
[self.contentView addSubview:self.scrollView];
[self.contentView addSubview:pageControl];
isAnimated= animated;
return self;
}
- (void)configureGallery:(NSInteger)accommodationId{
counter =0;
Accommodation *item = [[[DataManager sharedManager]accommodations]objectAtIndex:accommodationId];
numberPictures = [[item photo_set]count];
if (numberPictures >1) {
[scrollView setContentSize:CGSizeMake((numberPictures + 2)*320, 240)];
} else [scrollView setContentSize:CGSizeMake(320, 240)];
[pageControl setNumberOfPages:numberPictures];
// add the last image into the first position
if (numberPictures) {
[self addImageWithName:[NSString stringWithFormat:@"%@%@",[[DataManager sharedManager]baseUrl],[[item.photo_set objectAtIndex:numberPictures-1]thumbnail_gallery]] atPosition:0];
}
// add all of the images to the scroll view
for (int i = 0; i < numberPictures; i++) {
[self addImageWithName:[NSString stringWithFormat:@"%@%@",[[DataManager sharedManager]baseUrl],[[item.photo_set objectAtIndex:i]thumbnail_gallery]] atPosition:i+1 ];
}
// add the first image into the last position
[self addImageWithName:[NSString stringWithFormat:@"%@%@",[[DataManager sharedManager]baseUrl],[[item.photo_set objectAtIndex:0]thumbnail_gallery]] atPosition:numberPictures+1];
[self.scrollView scrollRectToVisible:CGRectMake(320,0,320,416) animated:NO];
[self performSelector:@selector(switchToNextPhoto) withObject:nil afterDelay:5];
}
- (void)addImageWithName:(NSString*)imageString atPosition:(int)position {
// add image to scroll view
UIImageView * placeHolderView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"France"]];
[placeHolderView setFrame:CGRectMake(position*320, 0, 320, 240)];
UIActivityIndicatorView * activityView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake((position*320)+160, 120, 20, 20)];
[activityView startAnimating];
EGOImageView *imageView = [[EGOImageView alloc] init];
imageView.frame = CGRectMake(position*320, 0, 320, 240);
[self.scrollView addSubview:placeHolderView];
[self.scrollView addSubview:activityView];
[self.scrollView addSubview:imageView];
imageView.imageURL = [NSURL URLWithString:imageString];
}
Table code for row 0
GalleryDetailCell *cell = (GalleryDetailCell *) [tableView dequeueReusableCellWithIdentifier: @"GalleryAnimated"];
if (cell == nil) {
cell = [[GalleryDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: @"GalleryAnimated" animated:animated];
}
[cell configureGallery:id];
cellToReturn = cell;
Have you got any idea where my mistake is? Do I have to manually handle my EGOImageView
and put them to nil
when I reload my cell??
Thank you for your time!
No correct solution
OTHER TIPS
EGOImageView can lead to memory leaks, be aware.
First of all, check if it releases _responseData in EGOImageLoadConnection's dealloc so that it looks like:
- (void)dealloc
{
self.response = nil;
self.delegate = nil;
[_connection release];
[_imageURL release];
[_responseData release]; //this line is absent in current EGOImageLoadConnection.m
[super dealloc];
}
Then call cancelImageLoad when deallocating a view which uses it and nil its image attribute (remember, I use ARC code sample):
- (void)dealloc
{
//...
self.myView.image = nil;
[self.myView cancelImageLoad];
//...
}
And I'd also recommend to call sometimes:
[EGOCache.currentCache clearCache];
Anyway, feel free to use Instruments. Open Allocations instrument, click VM Tracker and set automatic snapshots to 1 sec delay. Then watch Dirty memory column and Memory Tag 70 line, which shows how much memory images consume in your application.