iPhone SDK: rendering a CGLayer in an image object

advertisements

I am trying to add a curved border around an image downloaded and to be displayed in a UITableViewCell.

In the large view (ie one image on the screen) I have the following:

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];

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.

so in drawRect

- (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)];

So this works well, but if I remove the comment and try to show the rounded CA layer the scrolling goes really slow.

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:

productImageView.image = (UIImage*)[imageArray objectAtIndex:indexPath.row];

My question is "How do I render this layer into an image?" TIA.


This is what I got to work well.

- (UIImage *)roundedImage:(UIImage*)originalImage
{
    CGRect bounds = CGRectMake(0.0, 0.0, originalImage.size.width, originalImage.size.height);
    UIImageView *imageView = [[UIImageView alloc] initWithImage:originalImage];
    CALayer *layer = imageView.layer;
    imageView.frame = bounds;
    [layer setMasksToBounds:YES];
    [layer setCornerRadius:7.0];
    [layer setBorderWidth:2.0];
    [layer setBorderColor:[[UIColor darkGrayColor] CGColor]];
    UIGraphicsBeginImageContext(bounds.size);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *anImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [imageView release];

    return anImage;
}

Then to scale the image, I found this in the lazy loading example:

#define kAppIconHeight 48

    CGSize itemSize = CGSizeMake(kAppIconHeight, kAppIconHeight);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[image drawInRect:imageRect];
self.appRecord.appIcon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();