iOS中的圆角

2016-04-13  本文已影响146人  少少白

UIImageView的圆角:

    cell.imageOne.layer.cornerRadius = 30.0;
    cell.imageOne.layer.masksToBounds = YES;
    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
参考:

上一篇 下一篇

猜你喜欢

热点阅读