最近公司较闲, 没有前段的开发, 复习了一下iOS, 研究了一下贝塞尔曲线设置圆角.
写了一个UIImage的分类, 用的时候直接引入头文件, 可以动态设置边框宽度和颜色.
以下是分类.m文件
#import"UIImage+image.h"
@implementationUIImage (image)
/*
0.加载图片;
1.确定边框宽度;
2.开启一个上下文;
3.绘制大图显示出来;
4.绘制一个小圆,设置小圆为裁剪区域;
5.把图片绘制在上下文中;
6.从上下文中取出图片;
7.关闭上下文;
*/
+ (UIImage*_Nonnull)imageWithBorderW:(CGFloat)borderW color:(nonnullUIColor*)color image:(nonnullUIImage*)image{
CGSizesize =CGSizeMake(image.size.width+2* borderW, image.size.height+2* borderW);
UIGraphicsBeginImageContext(size);
UIBezierPath*path = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0, size.width, size.height)];
[colorset];
[pathfill];
UIBezierPath*clipPath = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(borderW, borderW, image.size.width, image.size.height)];
[clipPathaddClip];
[imagedrawAtPoint:CGPointMake(borderW, borderW)];
UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndPDFContext();
returnnewImage;
}
@end
以下是在ViewController的调用:
#import"ViewController.h"
#import"UIImage+image.h"
@interfaceViewController()
@property(weak,nonatomic)IBOutletUIImageView*imageV;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//0.加载图片
UIImage*image = [UIImageimageNamed:@"touxiang"];
self.imageV.image= [UIImageimageWithBorderW:5color:[UIColororangeColor]image:image];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end