我有一个UITableViewCell
子类,其中UIImage
为属性
我想在drawRect中绘制图像,并且效率很高,因为这是一个表视图单元格。
我知道如何绘制图像:
-(void)drawRect:(CGRect)aRect
{
CGRect rect = self.bounds;
[self.image drawInRect:rect];
}
但是如何画圆角图像并保持高效?
BTW我不想使用UIImageView
,然后设置图层角半径,因为它接缝会损害性能。
答案 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