I am presenting a two columned scrollview of images similar to Pinterest by using PSCollectionView https://github.com/ptshih/PSCollectionView and loading the images asynchronously using https://github.com/rs/SDWebImage
On iPhone 5 it is running continuously with no crashes, but on iPhone 4 I run into memory warnings and while scrolling down continually the app eventually crashes.
SDWebImage
claims they release cache upon receiving a memory warning and the PSCollectionView
manages the # of views by dequeuing reusable views, so I'm not sure what exactly is triggering the problems in memory.
I've ran memory profiling but I am not seeing anything that is glaringly obvious--the most memory intensive operation is coming from SDWebImage
loading the images asynchronously. It does seem like the objectForKey
method of getting data from my PFObjects
(from parse.com) also takes a good chunk of memory.
I've attached below in particular the code snippet that is setting the contents of each PSCollectionView
Cell.
Please let me know if you see any specific problems with the code that may lead to memory problems & also let me know if you have suggestions on how to better diagnose this memory problem.
- (UIView *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index withRect:(CGRect) recttouse {
FPCollectionViewCell *thefpcell = (FPCollectionViewCell *)
[collectionView dequeueReusableViewForClass:[FPCollectionViewCell class]];
thefpcell.frame = recttouse;
if (thefpcell == nil) {
thefpcell = [[FPCollectionViewCell alloc] initWithFrame:recttouse];
//NSLog(@"creating this cell: %i", index);
UILabel *cellText = [[UILabel alloc] initWithFrame:CGRectMake(2,0,143,20)];
thefpcell.cellText = cellText;
thefpcell.cellText.textAlignment = NSTextAlignmentCenter;
UIImageView *cellImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,20,recttouse.size.width, recttouse.size.height-20)];
thefpcell.cellImage = cellImage;
thefpcell.backgroundColor = [UIColor whiteColor];
thefpcell.cellText.font = [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:9];
thefpcell.cellText.backgroundColor = [UIColor clearColor];
[thefpcell addSubview:cellText];
[thefpcell addSubview:cellImage];
}
//@Brian note--consider moving more of this to other blocks to try and improve performance
[[NSOperationQueue mainQueue] addOperation:[NSBlockOperation blockOperationWithBlock:^{
PFObject *thisobj = [self.contentObjectsArray objectAtIndex:index];
thefpcell.cellText.frame = CGRectMake(2,0,143,20);
thefpcell.cellImage.frame = CGRectMake(0,20,recttouse.size.width, recttouse.size.height-20);
thefpcell.cellText.text = [thisobj objectForKey:@"Caption"];
NSString *imglink = [thisobj objectForKey:@"imgLink"];
NSString *imgurl;
if(imglink.length<2)
{
PFFile *mydata = [thisobj objectForKey:@"imageFile"];
imgurl = mydata.url;
}
else
{
imgurl =imglink;
}
UIImage *cellplaceholder = [UIImage imageWithContentsOfFile:@"placeholder.png"];
[thefpcell.cellImage setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:cellplaceholder];
thefpcell.layer.cornerRadius = 9.0;
thefpcell.layer.masksToBounds = YES;
}]];
//NSLog(@"got it: %i", index);
return thefpcell;
}