OC裁剪圆形图片加边框

saonian 发布于 18-11-26 00:49 已修改 364 点击
![alt text](https://www.shiqidu.com/upload/discussion/20181126/0222dcea562339298d9f9b29209a8a9d_origin.png) ```oc // 加载图片 UIImage *img = [UIImage imageNamed:@"165a2965fb1d8727"]; // 边框宽度 CGFloat borderW = 10; // 确定边框宽度 CGSize size = CGSizeMake(img.size.width + 2 * borderW, img.size.height + 2 * borderW); // 开启上下文 UIGraphicsBeginImageContextWithOptions(size, NO, 0); // 绘制大图,显示出来 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)]; [[UIColor blueColor] setFill]; [path fill]; // 绘制小圆,设置裁剪区域 UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderW, borderW, img.size.width, img.size.height)]; [clipPath addClip]; // 把图片绘制到上下文中 [img drawAtPoint:CGPointMake(borderW, borderW)]; // 从上下文中获取图片 UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文 UIGraphicsEndImageContext(); // 显示到页面中 self.imgView.image = newImg; ```
0 评论

请登录后发表评论

saonian

all or nothing, now or never.

文章
回复
粉丝