2017-11-08 15 views
-1

사용자가 그림을 그릴 때 최적의 선명도를 가져야하는이 서명보기를 어떻게 만들 수 있습니까? CoreGraphics을 사용하여 이미지에 그릴 때이 코드를 사용합니다. 현재이 같은오고 다른 싶습니다. 그걸 도와 줘. 아래는 코드 스 니펫입니다.이 서명보기를 더 선명하고 덜 흐리게 만들 수있는 방법은 무엇입니까?

나는이 링크

Have This 같은 이미지를 얻고있다.

나는이 링크

Want this 같은 더 명확한 이미지를 원한다.

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() 
    } 
} 
+0

https://github.com/GJNilsen/YPDrawSignatureView를 사용하십시오. 더 좋다고 생각합니다. –

+0

github에 많은 광고 모음이 있습니다. 이 것이 다른 것과 같이 달리는 경우에 그러나 좋을. –

+0

좋을지도 모릅니다. 나는 단지 내가 사용한 것을 제안했다. 필요한 경우 코드를 참조하고 수정할 수 있습니다. –

답변

0

그래픽 컨텍스트의 디테일 (대 픽셀)은 컨텍스트의 scale에 따라 다릅니다. 잘못 UIGraphicsBeginImageContext으로 전화를 걸어서 규모를 제공하지 못했습니다. 더 높은 등급을 제공 할 수 있도록 항상 UIGraphicsBeginImageContextWithOptions으로 전화하십시오. (너무 높게 만들지는 마십시오. 더 높은 규모의 컨텍스트에서는 더 많은 메모리가 필요합니다.)

+0

감사합니다. 그것은 효과가 있었다. 사실 저는 CoreGraphics에 익숙하지 않습니다. 그래서 저는 'UIGraphicsBeginImageContext'와 'UIGraphicsBeginImageContextWithOptions'의 차이점을 알지 못합니다. 지금 나는 알고있다 : D –