不使用layer的masksToBounds和cornerRadius来设置圆角

不使用layer的masksToBounds和cornerRadius来设置圆角:

/** 使用CoreGraphics和UIbezierPath */
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0.0);
    
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
    [imageView drawRect:imageView.bounds];
    
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();



/** 使用CAShapeLayer和UIBezierPath 性能好、渲染快速 */
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:imageView.bounds.size];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
 
    maskLayer.frame = imageView.bounds;
    maskLayer.path = maskPath.CGPath;
    imageView.layer.mask = maskLayer;



你可能感兴趣的:(OC)