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

Eya*_*yal 4 objective-c rounded-corners uitableview uiimage drawrect

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

我知道如何绘制图像:

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

    [self.image drawInRect:rect];
} 
Run Code Online (Sandbox Code Playgroud)

但是,如何绘制圆角图像并保持高效?

顺便说一句我不想使用UIImageView,然后设置图层角半径,因为它接缝会伤害性能.

Sve*_*sen 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);
Run Code Online (Sandbox Code Playgroud)