某些時候我們需要一些細(xì)節(jié)調(diào)整卤恳,可以自己自定義線,下面是畫直線的例子。同時提供 View 轉(zhuǎn) Image 方法葬凳,方便某些時候直接當(dāng)圖像使用。
/// 自定義線視圖
class LineView: UIView {
/// 快速創(chuàng)建 默認(rèn)全屏 height 高度
static func creatLineView(height height:CGFloat) -> LineView {
let lineView = LineView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: height))
lineView.backgroundColor = UIColor.clearColor()
return lineView
}
/// 畫直線
override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
CGContextSetAllowsAntialiasing(context, true)
CGContextSetLineWidth(context, self.frame.height)
CGContextSetRGBStrokeColor(context, 136/255, 136/255, 136/255, 1)// 顏色
CGContextBeginPath(context)
CGContextMoveToPoint(context, 0, 0); //起點(diǎn)坐標(biāo)
CGContextAddLineToPoint(context, self.frame.size.width, 0) //終點(diǎn)坐標(biāo)
CGContextStrokePath(context)
}
}
extension LineView {
/// View -> UIImage
func convertViewToImage() -> UIImage {
let size = self.bounds.size
//UIGraphicsBeginImageContext(size)
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.mainScreen().scale)
self.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者