1.方法一、
//size是背景图尺寸,border color是边框颜色,border width是边框大小。
extension UIImage {
class public func getImageWithLineBroken(size:CGSize,borderColor:UIColor,borderWidth:CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0);
UIColor.clearColor().set()
let context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextSetLineWidth(context, borderWidth);
CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
let lengths:[CGFloat] = [3, 1];
CGContextSetLineDash(context, 0, lengths, 1);
// line 1
CGContextMoveToPoint(context, 30.0, 0.0);
CGContextAddLineToPoint(context, 170, 0.0);
// line 2
CGContextAddArcToPoint(context, 200 , 30, 170, 60, 30)
// line 3
//CGContextAddLineToPoint(context, 200, 30);
// line 4
CGContextAddLineToPoint(context, 170, 60);
CGContextAddLineToPoint(context, 30, 60);
CGContextAddArcToPoint(context, 0 , 30, 30, 0, 30)
CGContextAddLineToPoint(context, 30, 0);
// CGContextAddArc(context, 170, 30, 30, CGFloat(M_PI * 270/180), CGFloat(M_PI_2), 0)
// //CGContextMoveToPoint(context, 170, 60);
// CGContextAddLineToPoint(context, 30, 60);
//
// CGContextAddArc(context, 30, 30, 30, CGFloat(M_PI_2), CGFloat(M_PI * 270/180), 0)
// CGContextMoveToPoint(context, 170, 60);
CGContextStrokePath(context);
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image
}