You can define a path, which is a hexagon with rounded corners (defining this path manually), and then apply this as a mask and border sublevel:
CGFloat lineWidth = 5.0; UIBezierPath *path = [UIBezierPath polygonInRect:self.imageView.bounds sides:6 lineWidth:lineWidth cornerRadius:30];
If the path to a regular rounded polygon can be implemented in this category:
@interface UIBezierPath (Polygon) + (instancetype)polygonInRect:(CGRect)rect sides:(NSInteger)sides lineWidth:(CGFloat)lineWidth cornerRadius:(CGFloat)cornerRadius; @end
and
@implementation UIBezierPath (Polygon) + (instancetype)polygonInRect:(CGRect)rect sides:(NSInteger)sides lineWidth:(CGFloat)lineWidth cornerRadius:(CGFloat)cornerRadius { UIBezierPath *path = [UIBezierPath bezierPath]; CGFloat theta = 2.0 * M_PI / sides;
This gives something like this:

Or in Swift 3 you can do:
let lineWidth: CGFloat = 5 let path = UIBezierPath(polygonIn: imageView.bounds, sides: 6, lineWidth: lineWidth, cornerRadius: 30) let mask = CAShapeLayer() mask.path = path.cgPath mask.lineWidth = lineWidth mask.strokeColor = UIColor.clear.cgColor mask.fillColor = UIColor.white.cgColor imageView.layer.mask = mask let border = CAShapeLayer() border.path = path.cgPath border.lineWidth = lineWidth border.strokeColor = UIColor.black.cgColor border.fillColor = UIColor.clear.cgColor imageView.layer.addSublayer(border)
FROM
extension UIBezierPath {
For Swift 2, see the previous version of this answer .
Rob
source share