title: iOS UIScrollView performance
body: <p>I'm trying to increase the scrolling performance of my <code>UIScrollView</code>. I have a lot of <code>UIButtons</code> on it (they could be hundreds): every button has a <strong>png</strong> image set as background.</p>
  
  <p>If I try to load the entire scroll when it appears, it takes too much time. Searching on the web, I've found a way to optimize it (loading and unloading pages <em>while</em> scrolling), but there's a little pause in scrolling everytime I have to load a new page.</p>
  
  <p>Do you have any advice to make it scroll smoothly?</p>
  
  <p>Below you can find my code.</p>
  
  <pre><code>- (void)scrollViewDidScroll:(UIScrollView *)tmpScrollView {
      CGPoint offset = tmpScrollView.contentOffset;
      //322 is the height of 2*2 buttons (a page for me)
      int currentPage=(int)(offset.y / 322.0f);
      if(lastContentOffset&gt;offset.y){
          pageToRemove = currentPage+3;
          pageToAdd = currentPage-3;
      }
      else{
          pageToRemove = currentPage-3;
          pageToAdd = currentPage+3;
      }
      //remove the buttons outside the range of the visible pages
      if(pageToRemove&gt;=0 &amp;&amp; pageToRemove&lt;=numberOfPages &amp;&amp; currentPage&lt;=numberOfPages){
          for (UIView *view in scrollView.subviews)
          {
              if ([view isKindOfClass:[UIButton class]]){
                  if(lastContentOffset&lt;offset.y &amp;&amp; view.frame.origin.y&lt;pageToRemove*322){
                      [view removeFromSuperview];
                  }
                  else if(lastContentOffset&gt;offset.y &amp;&amp; view.frame.origin.y&gt;pageToRemove*322){
                      [view removeFromSuperview];
                  }
              }
          }
      }
      if(((lastContentOffset&lt;offset.y &amp;&amp; lastPageToAdd+1==pageToAdd) || (lastContentOffset&gt;offset.y &amp;&amp; lastPageToAdd-1==pageToAdd)) &amp;&amp; pageToAdd&gt;=0 &amp;&amp; pageToAdd&lt;=numberOfPages){
          int tmpPage=0;
          if((lastContentOffset&lt;offset.y &amp;&amp; lastPageToAdd+1==pageToAdd)){
              tmpPage=pageToAdd-1;
          }
          else{
              tmpPage=pageToAdd;
          }
          //the images are inside the application folder
          NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
          for(int i=0;i&lt;4;i++){
              UIButton* addButton=[[UIButton alloc] init];
              addButton.layer.cornerRadius=10.0;
              if(i + (tmpPage*4)&lt;[imagesCatalogList count]){
                  UIImage* image=[UIImage imageWithContentsOfFile:[NSString stringWithFormat: @"%@/%@",docDir,[imagesCatalogList objectAtIndex:i + (tmpPage*4)]]];
                  if(image.size.width&gt;image.size.height){
                      image=[image scaleToSize:CGSizeMake(image.size.width/(image.size.height/200), 200.0)];
                      CGImageRef ref = CGImageCreateWithImageInRect(image.CGImage, CGRectMake((image.size.width-159.5)/2,(image.size.height-159.5)/2, 159.5, 159.5));
                      image = [UIImage imageWithCGImage:ref];
                  }
                  else if(image.size.width&lt;image.size.height){
                      image=[image scaleToSize:CGSizeMake(200.0, image.size.height/(image.size.width/200))];
                      CGImageRef ref = CGImageCreateWithImageInRect(image.CGImage, CGRectMake((image.size.width-159.5)/2, (image.size.height-159.5)/2, 159.5, 159.5));
                      image = [UIImage imageWithCGImage:ref];
                  }
                  else{
                      image=[image scaleToSize:CGSizeMake(159.5, 159.5)];
                  }
  
  
                  [addButton setBackgroundImage:image forState:UIControlStateNormal];
                  image=nil;
                  addButton.frame=CGRectMake(width, height, 159.5, 159.5);
                  NSLog(@"width %i height %i", width, height);
                  addButton.tag=i + (tmpPage*4);
                  [addButton addTarget:self action:@selector(modifyImage:) forControlEvents:UIControlEventTouchUpInside];
                  [tmpScrollView addSubview:addButton];
                  addButton=nil;
                  photos++;
              }
          }
      }
      lastPageToAdd=pageToAdd;
      lastContentOffset=offset.y;
  }
  </code></pre>