Goal c - Draw an image with rounded corners in drawRect

I have a subclass of UITableViewCell with the UIImage as property.
I want to draw an image in drawRect and be effective because it is a table cell.

I know how to draw an image like this:

 -(void)drawRect:(CGRect)aRect { CGRect rect = self.bounds; [self.image drawInRect:rect]; } 

But how can I draw an image with rounded corners and remain effective?

By the way, I do not want to use UIImageView , and then set the radius of the corner of the layer, because it throws harm to performance.

+4
source share
2 answers

You need to create a clipping path:

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

This link briefly describes the use of CALayer. How to draw rounded rectangles, borders and shadows.

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

+1
source

Source: https://habr.com/ru/post/1415132/


All Articles