我怎样才能让这个签名视图更清晰,更少模糊?
How can i make this signature view more clear and less blur?
我怎样才能使这个签名视图在用户绘制时具有最佳的清晰度。我使用此代码使用 CoreGraphics
在图像上绘制。目前它是这样的,我想要另一个。帮我解决这个问题。下面是代码片段。
我得到这样的图像link
.
我想要这样的图片更清晰link
.
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
以便您可以提供更高的比例。 (不过不要让它太高;更大规模的上下文需要成倍增加的内存。)
我怎样才能使这个签名视图在用户绘制时具有最佳的清晰度。我使用此代码使用 CoreGraphics
在图像上绘制。目前它是这样的,我想要另一个。帮我解决这个问题。下面是代码片段。
我得到这样的图像link
我想要这样的图片更清晰link
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
以便您可以提供更高的比例。 (不过不要让它太高;更大规模的上下文需要成倍增加的内存。)