2016-06-23 4 views
1

나는 손가락으로 선을 그릴 수있는 게임을 만들고 있습니다. 웹 사이트에는 수많은 방법이 있습니다. UIView (UIKit)에서 CGContext를 사용하는 방법과 SpriteKit에서 CGPath 및 SKShapeNode를 사용하는 두 가지 방법을 시도했습니다. 그러나 후자는 더 나은 품질을 보여줍니다. CGContext를 사용하는 첫 번째 것은 거친 가장자리를 가지고 있습니다.iOS CGContext를 사용하는 그리기 선의 품질이 SpriteKit의 SKShapeNode보다 훨씬 나쁩니다.

다음 스크린 샷을 참조하십시오. 또한 두 가지 방법에 대한 코드 일부를 여기에 첨부했습니다 (touchesMove 함수에 있음).

참고 : var ref = CGPathCreateMutable()

CGContext있는 UIView에서 enter image description here

CGPathAddLineToPoint(ref, nil, currentPoint.x, currentPoint.y) 

    UIGraphicsBeginImageContext(self.frame.size) 
    let context = UIGraphicsGetCurrentContext() 
    tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)) 


    CGContextAddPath(context, ref) 

    // 3 
    CGContextSetLineCap(context, CGLineCap.Round) 
    CGContextSetLineWidth(context, brushWidth) 
    CGContextSetRGBStrokeColor(context, red, green, blue, 1.0) 
    CGContextSetBlendMode(context, CGBlendMode.Normal) 

    // 4 
    CGContextStrokePath(context) 

    // 5 
    UIGraphicsEndImageContext() 

SpriteKit에서 SKShapeNode enter image description here 참고 : var lineDrawing = SKShapeNode()

CGPathAddLineToPoint(ref, nil, location.x, location.y) 
lineDrawing.path = ref 
lineDrawing.lineWidth = 3 
lineDrawing.strokeColor = UIColor.blackColor() 
lineDrawing.alpha = 1.0 
self.addChild(lineDrawing) 

동일한 품질의 SKShapeNode로 UIView에서 선을 그리려면 어떻게해야합니까? ,

UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0) 

WithOptions 버전 :

UIGraphicsBeginImageContext(self.frame.size) 

당신이 사용하는 대신해야합니다

+0

실제 경로가 인쇄되어 있습니다. 내 추측으로는 각기 다른 수의 경로가 있다는 것입니다. –

답변

1

하나의 명백한 문제는 레티 나 화면을 처리하지 않는 오래된 기능을 사용하고 있다는 것입니다 최종 인수로 0을 사용하면 장치에 망막 화면이 있으면 망막 해상도로 이미지 컨텍스트가 생성됩니다. (0 인수는 "장치 화면의 축척 비율 사용"을 의미합니다.)

각 테스트 사례의 경로에 포인트를 생성하는 코드를 표시하지 않았기 때문에 다른 문제가있을 수 있습니다.

+0

SKShapeNode만큼 좋지만 여전히 좋지는 않습니다. 경로 생성을위한 코드는 다음과 같이 거기에 보여집니다 : (1)'var ref = CGPathCreateMutable()'; (2) touchesBegan에서,'CGPathMoveToPoint (ref, nil, location.x, location.y)'; (3) touchesMove,'CGPathAddLineToPoint (ref, nil, location.x, location.y)'에 있습니다. 내가 놓친 게 있니? –

+0

위치가'let location = touch.locationInView (self)' –

+0

두 테스트에서 같은 속도로 같은 모양을 그리십시오. 'CGContext' 구현에서 얼마나 많은 포인트가 끝날까요? SpriteKit 버전은 몇 개입니까? –