Ios 如何使此签名视图更清晰、更模糊?

Ios 如何使此签名视图更清晰、更模糊?,ios,swift,xcode,core-graphics,Ios,Swift,Xcode,Core Graphics,当用户在其上绘图时,如何使此签名视图具有最佳清晰度。我使用此代码使用CoreGraphics在图像上绘制。现在是这样,我想要另一个。帮我吧。下面是代码片段 我得到的图像与此链接类似 我希望图像像此链接一样更清晰 var lastPoint:CGPoint! 维平:呸! var isEditedImage=false 变量线宽:CGFloat=3.0 变量lineOpacity:CGFloat=1.0 var lineColor=UIColor.black 覆盖功能触摸开始(Touchs:Se

当用户在其上绘图时,如何使此签名视图具有最佳清晰度。我使用此代码使用
CoreGraphics
在图像上绘制。现在是这样,我想要另一个。帮我吧。下面是代码片段

我得到的图像与此链接类似

我希望图像像此链接一样更清晰

var lastPoint:CGPoint!
维平:呸!
var isEditedImage=false
变量线宽:CGFloat=3.0
变量lineOpacity:CGFloat=1.0
var lineColor=UIColor.black
覆盖功能触摸开始(Touchs:Set,
带事件:UIEvent?){
isSwiping=false
如果让触摸=先触摸{
lastPoint=触摸位置(在:signView中)
}
}
覆盖功能触摸移动(touchs:Set,
带事件:UIEvent?){
正在擦除=真
如果让触摸=先触摸{
让currentPoint=touch.location(在:signView中)
UIGraphicsBeginImageContext(self.signView.frame.size)
self.signView.image?.draw(in:CGRect(x:0,y:0,宽度:self.signView.frame.size.width,高度:self.signView.frame.size.height))
UIGraphicsGetCurrentContext()?.move(到:CGPoint(x:lastPoint.x,y:lastPoint.y))
UIGraphicsGetCurrentContext()?.addLine(到:CGPoint(x:currentPoint.x,y:currentPoint.y))
UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
UIGraphicsGetCurrentContext()?.setLineWidth(线宽)
UIGraphicsGetCurrentContext()?.setAlpha(线条不透明度)
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor.cgColor)
UIGraphicsGetCurrentContext()?.beginTransparencyLayer(辅助信息:nil)
UIGraphicsGetCurrentContext()?.strokePath()的
UIGraphicsGetCurrentContext()?.endTransparencyLayer()
self.signView.image=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
lastPoint=currentPoint
isEditedImage=true
}
}
覆盖函数touchesend(utouchs:Set,
带事件:UIEvent?){
如果(!正在擦除){
//这是一个单点触摸,画一个点
UIGraphicsBeginImageContext(self.signView.frame.size)
self.signView.image?.draw(in:CGRect(x:0,y:0,宽度:self.signView.frame.size.width,高度:self.signView.frame.size.height))
UIGraphicsGetCurrentContext()?.setLineCap(CGLineCap.round)
UIGraphicsGetCurrentContext()?.setLineWidth(线宽)
UIGraphicsGetCurrentContext()?.setAlpha(线条不透明度)
UIGraphicsGetCurrentContext()?.setStrokeColor(lineColor.cgColor)
UIGraphicsGetCurrentContext()?.beginTransparencyLayer(辅助信息:nil)
UIGraphicsGetCurrentContext()?.strokePath()的
UIGraphicsGetCurrentContext()?.endTransparencyLayer()
self.signView.image=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
}
}

图形上下文中的细节量(相对于像素化)取决于上下文的
比例。您错误地调用了
UIGraphicsBeginImageContext
,因此无法提供刻度。始终使用选项调用
UIGraphicsBeginImageContextWithOptions
,以便提供更高的比例。(不过,不要把它设置得太高;更高级别的上下文需要指数级的内存。)

使用我认为更好的方法github中有很多pod。但是,如果这辆车能像其他车一样跑,那就太好了。我只是建议我用过的。如果需要,您可以参考代码并更正。您链接的两张图片是相同的。谢谢matt。成功了。事实上,我是CoreGraphics新手,因此我不知道“UIGraphicsBeginImageContext”和“UIGraphicsBeginImageContextWithOptions”之间的区别。现在我知道了:D
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()
    }
}