美文网首页
iOS中的圆角

iOS中的圆角

作者: 少少白 | 来源:发表于2016-04-13 20:11 被阅读146次

    UIImageView的圆角:

    • 产生离屏渲染的maskToBounds
        cell.imageOne.layer.cornerRadius = 30.0;
        cell.imageOne.layer.masksToBounds = YES;
    
    • 添加maskView
        CALayer *maskLayer = [[CALayer alloc] init];
        maskLayer.frame = CGRectMake(0, 0, 60, 60);
        maskLayer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"appIcon_placeholder"].CGImage);
        cell.imageOne.layer.mask = maskLayer; // iOS 7
        cell.imageTwo.maskView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appIcon_placeholder"]]; // iOS 8
        cell.imageThree.maskView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appIcon_placeholder"]];
    
    • 生成一张圆角图片
    - (UIImage *)cornerWithImage:(UIImage *)image cornerRadius:(CGFloat)cornerRadius andSize:(CGSize)size
    {
        CGRect rect = CGRectMake(0, 0, size.width, size.height);
        UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
        CGContextRef contextRef = UIGraphicsGetCurrentContext();
        UIBezierPath  *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        CGContextAddPath(contextRef, bezierPath.CGPath);
        CGContextClip(contextRef);
        [image drawInRect:rect];
        CGContextDrawPath(contextRef, kCGPathFillStroke);
        UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return resultImage;
    }
    

    Demo地址MyCornerRadius
    参考:

    相关文章

      网友评论

          本文标题:iOS中的圆角

          本文链接:https://www.haomeiwen.com/subject/okqglttx.html