当用户在其上绘图时,如何制作应具有最佳清晰度的签名视图。我使用此代码使用CoreGraphics
. 目前它是这样的,我想要另一个。帮我解决这个问题。下面是代码片段。
我得到像这个链接的图像
.
我希望图像更清晰,就像这个链接
.
var lastPoint:CGPoint!
var isSwiping:Bool!
var isEditedImage = false
var lineWidth:CGFloat = 3.0
var lineOpacity:CGFloat = 1.0
var lineColor = UIColor.black
override func touchesBegan(_ touches: Set<UITouch>,
with event: UIEvent?){
isSwiping = false
if let touch = touches.first{
lastPoint = touch.location(in: signView)
}
}
override func touchesMoved(_ touches: Set<UITouch>,
with event: UIEvent?){
isSwiping = true
if let touch = touches.first{
let currentPoint = touch.location(in: signView)
UIGraphicsBeginImageContext(self.signView.frame.size)
self.signView.image?.draw(in: CGRect(x: 0, y: 0, width: self.signView.frame.size.width, height: self.signView.frame.size.height))
UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: currentPoint.x, y: currentPoint.y))
UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
UIGraphicsGetCurrentContext()?.setLineWidth(lineWidth)
UIGraphicsGetCurrentContext()?.setAlpha(lineOpacity)
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor.cgColor)
UIGraphicsGetCurrentContext()?.beginTransparencyLayer(auxiliaryInfo: nil)
UIGraphicsGetCurrentContext()?.strokePath()
UIGraphicsGetCurrentContext()?.endTransparencyLayer()
self.signView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
lastPoint = currentPoint
isEditedImage = true
}
}
override func touchesEnded(_ touches: Set<UITouch>,
with event: UIEvent?){
if(!isSwiping) {
// This is a single touch, draw a point
UIGraphicsBeginImageContext(self.signView.frame.size)
self.signView.image?.draw(in: CGRect(x: 0, y: 0, width: self.signView.frame.size.width, height: self.signView.frame.size.height))
UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
UIGraphicsGetCurrentContext()?.setLineWidth(lineWidth)
UIGraphicsGetCurrentContext()?.setAlpha(lineOpacity)
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor.cgColor)
UIGraphicsGetCurrentContext()?.beginTransparencyLayer(auxiliaryInfo: nil)
UIGraphicsGetCurrentContext()?.strokePath()
UIGraphicsGetCurrentContext()?.endTransparencyLayer()
self.signView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
}
图形上下文中的细节量(相对于像素化)取决于上下文的scale
. 您错误地调用了UIGraphicsBeginImageContext
,因此您没有提供秤。总是打电话,UIGraphicsBeginImageContextWithOptions
这样你就可以提供更高的规模。(不过不要让它太高;更大规模的上下文需要成倍增加的内存。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句