iPhone SDK:将CGLayer渲染到图像对象中

时间:2010-03-05 00:33:13

标签: iphone cocoa-touch uikit core-graphics

我试图在下载的图像周围添加一个弯曲的边框,并在UITableViewCell中显示。

在大视图中(即屏幕上的一个图像),我有以下内容:

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

在表格视图单元格中,要使其快速滚动,需要在UIView的drawRect方法中绘制图像,然后将其添加到自定义单元格中。

所以在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)];

所以这很好用,但是如果我删除评论并尝试显示圆形CA层,则滚动速度非常慢。

要解决此问题,我想我必须将此图像上下文渲染到另一个图像对象中,并将其存储在一个数组中,然后将此图像设置为:

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

我的问题是“如何将此图层渲染为图像?” TIA。

2 个答案:

答案 0 :(得分:1)

这是我工作得很好的原因。

- (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;
}

然后为了缩放图像,我在延迟加载示例中找到了这个:

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

答案 1 :(得分:0)

您正走在正确的轨道上,并附有自己的评论。

- (UIImage *)roundedImage:(UIImage*)originalImage;
{
    CGRect bounds = originalImage.bounds;
    CGImageRef theImage = originalImage.CGImage;
    CALayer *roundedlayer = [CALayer layer];
    roundedlayer.position = CGPointMake(0.0f,0.0f);
    roundedlayer.bounds = bounds;
    [roundedlayer setMasksToBounds:YES];
    [roundedlayer setCornerRadius:7.0];
    [roundedlayer setBorderWidth:2.0];
    [roundedlayer setBorderColor:[[UIColor darkGrayColor] CGColor]];
    roundedlayer.contents = theImage;

    UIGraphicsBeginImageContext(bounds.size);       // creates a new context and pushes it on the stack

    CGContextBeginTransparencyLayerWithRect(UIGraphicsGetCurrentContext(), bounds, NULL);
    CGContextClearRect(UIGraphicsGetCurrentContext(), bounds);
    [roundedlayer drawInContext:UIGraphicsGetCurrentContext()];

    UIImage *anImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext();        //releases new context and removes from stack

    return anImage;
}

我会预先渲染这些并将它们存储在您的图像数组中,这样它们就不会在drawRect中计算,而是在

中设置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

方法