设置圆形图片

设置圆形图片,有两种方式,具体如下:

1. 改变尺寸,在视觉上是圆形
_midImage.layer.cornerRadius = _midImage.width/2;
_midImage.layer.masksToBounds = YES;
2.绘制一个圆形图片
+ (UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {

    UIGraphicsBeginImageContext(image.size);

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context,inset);

    CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);

    CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset *2.0f);
    CGContextAddEllipseInRect(context, rect);

    CGContextClip(context);

    //在圆区域内画出image原图

    [image drawInRect:rect];

    CGContextAddEllipseInRect(context, rect);

    CGContextStrokePath(context);

    //生成新的image

    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newimg;
}

你可能感兴趣的:(设置圆形图片)