//設(shè)置圖片
self.DottedLine.image = [self imageWithLineWithImageView:self.DottedLine];
//畫虛線方法
-(UIImage *)imageWithLineWithImageView:(UIImageView *)imageView{
CGFloat width = imageView.frame.size.width;
CGFloat height = imageView.frame.size.height;
UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.image drawInRect:CGRectMake(0, 0, width, height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGFloat lengths[] = {5,5};//虛線的長(zhǎng)度設(shè)置
CGContextRef line = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(line, [UIColor colorWithRed:133/255.0 green:133/255.0 blue:133/255.0 alpha:1.0].CGColor);
//畫線
CGContextSetLineDash(line, 0, lengths, 1);
CGContextMoveToPoint(line, 0, 1);
CGContextAddLineToPoint(line, width-5, 1);
CGContextStrokePath(line);
return UIGraphicsGetImageFromCurrentImageContext();
}
效果圖

68B0C875-4E58-43E0-99E6-530CA9D16220.png
Alert:
只是測(cè)試了這一種方法,還有其他的方法,回頭測(cè)試了再貼出來(lái)