在 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;
}
声明:此博文源自 http://blog.csdn.net/shenyuanluo/article/details/49122927
如需转载,请说明博文出处。谢谢!
本文介绍了一种在iOS开发中将矩形图片转换为圆形图片的方法,并提供了详细的代码实现过程,适用于需要创建圆形图片特效的应用场景。
3972

被折叠的 条评论
为什么被折叠?



