iOS给一张矩形图片剪切成圆形图片

随着APP发展,个人账户的注册和登陆,都有头像的设置,圆形头像也越来越多,此方法正是对剪切圆头像的封装。 
 
 
//****************************************************************************************************************//
//****************************************************************************************************************//

//*********************************************图片剪切成圆形********************************************************//

//****************************************************************************************************************//

//**********************************************************************笨笨编程************************************//

//****************************************************************************************************************//

/**

 * parm:sourceImage:需要剪切的原图片

 * parm:borderWidth:剪切后的边框宽度

 * parm:borderColor:边框颜色

 */

- (UIImage *)circleImageWithImage:(UIImage *)sourceImage borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor{

    CGFloat imageWidth = sourceImage.size.width + 2 * borderWidth;

    CGFloat imageHeight = sourceImage.size.height + 2 * borderWidth;

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageWidth, imageHeight), NO, 0.0);

    UIGraphicsGetCurrentContext();

    CGFloat radius = (sourceImage.size.width < sourceImage.size.height?sourceImage.size.width:sourceImage.size.height)*0.5;

    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(imageWidth * 0.5, imageHeight * 0.5) radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES];

    bezierPath.lineWidth = borderWidth;

    [borderColor setStroke];

    [bezierPath stroke];

    [bezierPath addClip];

    [sourceImage drawInRect:CGRectMake(borderWidth, borderWidth, sourceImage.size.width, sourceImage.size.height)];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();   

    UIGraphicsEndImageContext();    

    return image;

}

posted @ 2015-07-20 15:59  笨笨编程  阅读(1363)  评论(0编辑  收藏  举报