目标c - 在drawRect中绘制带圆角的图像

时间:2012-05-30 10:43:22

标签: objective-c uitableview uiimage rounded-corners drawrect

我有一个UITableViewCell子类,其中UIImage为属性 我想在drawRect中绘制图像,并且效率很高,因为这是一个表视图单元格。

我知道如何绘制图像:

-(void)drawRect:(CGRect)aRect 
{
    CGRect rect = self.bounds;

    [self.image drawInRect:rect];
} 

但是如何画圆角图像并保持高效?

BTW我不想使用UIImageView,然后设置图层角半径,因为它接缝会损害性能。

2 个答案:

答案 0 :(得分:16)

您需要创建剪切路径:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath;
CGContextAddPath(ctx, clippath);
CGContextClip(ctx);
[self.image drawInRect:rect];
CGContextRestoreGState(ctx);

答案 1 :(得分:1)

此链接简要介绍了如何使用CALayer。如何绘制圆角矩形,边框和阴影。

http://www.raywenderlich.com/2502/introduction-to-calayers-tutorial