swift 给UIView添加圆角,避免离屏渲染

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 给UIView添加圆角,避免离屏渲染相关的知识,希望对你有一定的参考价值。

@interface UIView (AddCorner)

/**
 Add Corner without border.

 @discussion It only support to add corner without border.
 If with border, use `addCorner:borderWidth:borderColor:`
 */
- (void)addCorner:(CGFloat)radius;

/**
 Add Corner with border.

 @discussion Can't use the system border after use it to show a border.
 */
- (void)addCorner:(CGFloat)radius
      borderWidth:(CGFloat)borderWidth
      borderColor:(UIColor*)borderColor;

@end
@implementation UIView (AddCorner)

- (void)addCorner:(CGFloat)radius {
    UIImageView * imageView = [[UIImageView alloc] initWithImage:[self drawRoundedCorner:radius
                                                                             borderWidth:0
                                                                             borderColor:nil]];
    [self insertSubview:imageView atIndex:0];
    self.backgroundColor = [UIColor clearColor];
}

- (void)addCorner:(CGFloat)radius
      borderWidth:(CGFloat)borderWidth
      borderColor:(UIColor *)borderColor {
    UIImageView * imageView = [[UIImageView alloc] initWithImage:[self drawRoundedCorner:radius
                                                                             borderWidth:borderWidth
                                                                             borderColor:borderColor]];
    [self insertSubview:imageView atIndex:0];
    self.backgroundColor = [UIColor clearColor];
}

- (UIImage *)drawRoundedCorner:(CGFloat)radius
                   borderWidth:(CGFloat)borderWidth
                   borderColor:(UIColor *)borderColor {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, [UIScreen mainScreen].scale);
    CGContextRef context =UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, borderWidth);
    CGContextSetStrokeColorWithColor(context, borderColor.CGColor ?: self.backgroundColor.CGColor);
    CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor ?: [UIColor clearColor].CGColor);
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;

    // From Right-Top point
    CGContextMoveToPoint(context, width, radius);
    // Go to Right-Bottom point
    CGContextAddArcToPoint(context, width, height, width - radius, height, radius);
    // Go to Left-Bottom point
    CGContextAddArcToPoint(context, 0, height, 0, height - radius, radius);
    // Go to Left-Top point
    CGContextAddArcToPoint(context, 0, 0, width, 0, radius);
    // Go to RIght-Top Point
    CGContextAddArcToPoint(context, width, 0, width, radius, radius);

    CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
    UIImage * contentImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return contentImg;
}

@end

以上是关于swift 给UIView添加圆角,避免离屏渲染的主要内容,如果未能解决你的问题,请参考以下文章

iOS给UIImageView添加圆角的三种方法

Swift高效设置圆角图片的方法

iOS圆角的离屏渲染,你真的弄明白了吗

iOS沉思录UIImage圆角矩形的‘离屏渲染’和‘在屏渲染’实现方法

离屏渲染

iOS中设置圆角的方式