How can I make this signature view that should have optimal clarity when user drawing on it. I use this code to draw on image using CoreGraphics
. Currently it coming like this and I want like another one. Help me with that. Below is the code snippet .
I am getting image like this link
.
I want image to more clear like this 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()
}
}
The amount of detail (vs. pixellation) in a graphics context depends on the context's scale
. You are wrongly calling UIGraphicsBeginImageContext
, so you are failing to provide a scale. Always call UIGraphicsBeginImageContextWithOptions
so that you can provide a higher scale. (Don't make it too high, though; a higher-scale context requires exponentially more memory.)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments