title: iPhone SDK: Rendering a CGLayer into an image object
body: <p>I am trying to add a curved border around an image downloaded and to be displayed in a UITableViewCell. </p>
  
  <p>In the large view (ie one image on the screen) I have the following: </p>
  
  <pre><code>productImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:product.image]];
  [productImageView setAlpha:0.4];
  
  productImageView.frame = CGRectMake(10.0, 30.0, 128.0, 128.0);
  CALayer *roundedlayer = [productImageView layer];
  [roundedlayer setMasksToBounds:YES];
  [roundedlayer setCornerRadius:7.0];
  [roundedlayer setBorderWidth:2.0];
  [roundedlayer setBorderColor:[[UIColor darkGrayColor] CGColor]];
  [self addSubview:productImageView];
  </code></pre>
  
  <p>In the table view cell, to get it to scroll fast, an image needs to be drawn in the drawRect method of a UIView which is then added to a custom cell. </p>
  
  <p>so in drawRect</p>
  
  <pre><code>- (void)drawRect:(CGRect)rect {
  
  ...
  
  point = CGPointMake(boundsX + LEFT_COLUMN_OFFSET, UPPER_ROW_TOP);
  
  //CALayer *roundedlayer = [productImageView layer];
  //[roundedlayer setMasksToBounds:YES];
  //[roundedlayer setCornerRadius:7.0];
  //[roundedlayer setBorderWidth:2.0];
  //[roundedlayer setBorderColor:[[UIColor darkGrayColor] CGColor]];
  //[productImageView drawRect:CGRectMake(boundsX + LEFT_COLUMN_OFFSET, UPPER_ROW_TOP, IMAGE_WIDTH, IMAGE_HEIGHT)];
  //      
  
  [productImageView.image drawInRect:CGRectMake(boundsX + LEFT_COLUMN_OFFSET, UPPER_ROW_TOP, IMAGE_WIDTH, IMAGE_HEIGHT)];
  </code></pre>
  
  <p>So this works well, but if I remove the comment and try to show the rounded CA layer the scrolling goes really slow.</p>
  
  <p>To fix this I suppose I would have to render this image context into a different image object, and store this in an array, then set this image as something like:</p>
  
  <pre><code>productImageView.image = (UIImage*)[imageArray objectAtIndex:indexPath.row];
  </code></pre>
  
  <p>My question is "How do I render this layer into an image?"
  TIA.</p>