title: Draw a grid of images using Core Graphics in drawRect:
body: <p>My problem is this: I have a simple block image that I want to make a grid out of.  I have created an array of CGPoints in my UIView.  Then I used</p>
  <pre><code>blackImage = [UIImage imageNamed:@&quot;black.png&quot;];
  </code></pre>
  <p>and then</p>
  <pre><code>- (void)drawRect:(CGRect)rect {
      for (int j = 0 ; j &lt;= 11; j++)
      {
          for (int i = 0 ; i &lt;= 7; i++) 
          {
              [blackImage drawAtPoint: (CGPointFromString([[self.points objectAtIndex:j] objectAtIndex:i]))];
          }
      }
      [whiteImage drawAtPoint:(CGPointMake((CGFloat)(floor((touchX+0.001)/40)*40), (CGFloat)(floor((touchY+0.001)/40))*40))];
  
      //  [whiteImage drawAtPoint:(CGPointMake(240.0, 320.0))];   
  }
  </code></pre>
  <p>Where TouchX and TouchY are the CGPoints where the user has touched the screen.  So I'm basically displaying a different image at the point on the grid where the user touched.</p>
  <p>First of all, the problem is that the whole screen is redrawn every time I call DrawRect.  I want to be able to save the state (by changing an array of BOOL's?) and have the user drag on the screen and change multiple images.  However, I'm unable to use the &quot;drawAtPoint&quot; method on the UImage when I'm outside of DrawRect.  (throws an error).</p>
  <p>Anyhow, I started looking into the CoreAnimation and Quartz docs, and got really confused.   I'm not sure if I should be creating multiple UIViews for each brick (seems excessive), or a grid of CALayers or CGLayers...  I think it's pretty simple what I want to do... but I don't really understand Layers, and the difference between using a CALayer and a CGLayer.   Apparently &quot;All UIViews are layer backed&quot;  What does that mean?</p>
  <p>I'm kind of lost on how to implement this.  I basically want to swap images in the grid so that the user can essentially 'paint' across the screen and toggle the images.</p>
  <hr />
  <h2>Edit</h2>
  <p>I've tried implementing this using [self setNeedsDisplayinRect], and passing in the CGRect that is under the user's finger.  However, this is still calling DrawRect within TouchesMoved, which IS giving me the effect I want, but is way too slow on the iPhone.  Is there a more efficient way to accomplish the same effect using CALayers?  I'm new to Core Animation and don't really understand how to use a layer to solve this problem.</p>