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

来源:互联网 发布:淘宝卖什么不需要物流 编辑:程序博客网 时间:2024/06/05 14:19

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

/*!@function       convertToCircleWithImage:onWidth:onColor @discussion     Convert rectangle to circle with image . @paramrectangleImage                        source image    @param          width                        Border with after convert.@paramcolor                        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

如需转载,请说明博文出处。谢谢!

0 0
原创粉丝点击