您的位置:首页 > 移动开发 > IOS开发

iOS之矩形图片切割成圆形图片

2015-10-14 11:37 591 查看
在 iOS 开发中,有些情况往往需要对图片进行切割。比如说音频播放器中的专辑图片,需要显示成圆形转动效果,而图片资源往往都是矩形的,此时就很有必要把矩形图片切割成圆形。

/*!
@function       convertToCircleWithImage:onWidth:onColor

@discussion     Convert rectangle to circle with image .

@param			rectangleImage
source image
@param          width
Border with after convert.
@param			color
Color with after convert.
*/
+(UIImage *)convertToCircleWithImage:(UIImage *)rectangleImage
onWidth:(CGFloat)width
onColor:(UIColor *)color
{

CGFloat imageWidth = rectangleImage.size.width + 2 * width;
CGFloat imageHeight = rectangleImage.size.height + 2 * width;

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

UIGraphicsGetCurrentContext();

CGFloat radius = (rectangleImage.size.width < rectangleImage.size.height
? rectangleImage.size.width : rectangleImage.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 = width;
[color setStroke];
[bezierPath stroke];
[bezierPath addClip];

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

UIImage *circleImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return circleImage;
}


声明:此博文源自 /article/11517874.html

如需转载,请说明博文出处。谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: