iOS給一張矩形圖片剪切成圓形圖片

隨着APP發展,我的帳戶的註冊和登錄,都有頭像的設置,圓形頭像也愈來愈多,此方法正是對剪切圓頭像的封裝。 
 
 
//****************************************************************************************************************//
//****************************************************************************************************************//

//*********************************************圖片剪切成圓形********************************************************//編程

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

//**********************************************************************笨笨編程************************************//spa

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

/**圖片

 * parm:sourceImage:須要剪切的原圖片ip

 * parm:borderWidth:剪切後的邊框寬度ci

 * parm:borderColor:邊框顏色it

 */io

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

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

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

    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;

}

相關文章
相關標籤/搜索