title: iOS Memory Crashes UIScrollview with asynchronous image loading
body: <p>I am presenting a two columned scrollview of images similar to Pinterest by using PSCollectionView <a href="https://github.com/ptshih/PSCollectionView" rel="nofollow">https://github.com/ptshih/PSCollectionView</a> and loading the images asynchronously using <a href="https://github.com/rs/SDWebImage" rel="nofollow">https://github.com/rs/SDWebImage</a></p>
  
  <p>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.  </p>
  
  <p><code>SDWebImage</code> claims they release cache upon receiving a memory warning and the <code>PSCollectionView</code> manages the # of views by dequeuing reusable views, so I'm not sure what exactly is triggering the problems in memory.  </p>
  
  <p>I've ran memory profiling but I am not seeing anything that is glaringly obvious--the most memory intensive operation is coming from <code>SDWebImage</code> loading the images asynchronously. It does seem like the <code>objectForKey</code> method of getting data from my <code>PFObjects</code> (from parse.com) also takes a good chunk of memory.   </p>
  
  <p>I've attached below in particular the code snippet that is setting the contents of each <code>PSCollectionView</code> Cell.  </p>
  
  <p>Please let me know if you see any specific problems with the code that may lead to memory problems &amp; also let me know if you have suggestions on how to better diagnose this memory problem.</p>
  
  <pre><code>- (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&lt;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;
  }
  </code></pre>