標籤:etop return 起點 img draw ram bsp 多少 之間
繪製一條虛線,此方法可以寫到UIImage的分類中,直接寫成類方法調用即可。
1 - (UIImage *)drawDashImageWithFrame:(CGRect)frame andLineColor:(UIColor *)lineColor 2 { 3 // 開啟圖形上下文 4 UIGraphicsBeginImageContext(frame.size); 5 6 // 擷取上下文 7 CGContextRef cxtRef = UIGraphicsGetCurrentContext(); 8 9 // 設定線頭樣式10 CGContextSetLineCap(cxtRef, kCGLineCapRound);11 12 // 繪製3個點,跳過2個點,繪製3個點,跳過3個點,繪製2個點,如此反覆13 // CGFloat lengths[] = {3,2,3};14 // 6是每個虛線的長度,5是虛線之間的間隔15 CGFloat lengths[] = {6,5};16 17 // 設定線寬(向兩邊擴充)18 CGContextSetLineWidth(cxtRef,1);19 20 // 設定線條渲染顏色21 [lineColor setStroke];22 23 // 畫虛線24 // 參數2:第一次繪製的時候跳過多少個點25 // 參數3:數組26 // 參數4:數組長度27 CGContextSetLineDash(cxtRef, 0,lengths, 2);28 29 // 開始畫線-起點30 CGContextMoveToPoint(cxtRef, 0.0, 0.0);31 32 // 線束畫線-終點33 CGContextAddLineToPoint(cxtRef, [UIScreen mainScreen].bounds.size.width, 0.0);34 35 // 設定描邊36 CGContextStrokePath(cxtRef);37 38 // 從圖形上下文中擷取圖片39 UIImage *img = UIGraphicsGetImageFromCurrentImageContext();40 41 // 關閉圖形上下文42 UIGraphicsEndImageContext();43 44 return img;45 }
畫虛線