2016-11-07 29 views
0

我從這段代碼中得到了大量的CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.錯誤。它在SKScene中執行。基本上,我在屏幕上以SKShapeNode的形式獲取用戶的繪圖。然後我用Core Graphics的圓圈填充繪圖的路徑。然而,代碼運行緩慢,並給我噸錯誤。我對Swift很陌生。你能幫助我瞭解發生了什麼事嗎?我如何加快速度?我怎麼能給CoreGraphics適當的上下文?CoreGraphics將結果繪製到CGContextDrawPath:無效的上下文0x0

    let boundingBox = createdShape.frame 
        for j in stride(from: Int(boundingBox.minX), to: Int(boundingBox.maxX), by: 10) { 
         for i in stride(from: Int(boundingBox.minY), to: Int(boundingBox.maxY), by: 10) { 
         //for i in 0..<10 { 
          counter += 1 
          //let originalPoint = CGPoint(x: ((boundingBox.maxX - boundingBox.minX)/2)+boundingBox.minX, y: (boundingBox.maxY-CGFloat(i*10))) 
          //let originalPoint = CGPoint(x: CGFloat(j), y: (boundingBox.maxY-CGFloat(i*10))) 
          let originalPoint = CGPoint(x: CGFloat(j), y: (CGFloat(i))) 
          let point:CGPoint = self.view!.convert(originalPoint, from: self) 

          if (createdShape.path?.contains(createdShape.convert(originalPoint, from: self)))! { 
           let circlePath = UIBezierPath(arcCenter: point, radius: CGFloat(5), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true) 
           circlePath.fill() 

           let shapeLayer = CAShapeLayer() 
           shapeLayer.path = circlePath.cgPath 

           shapeLayer.fillColor = UIColor(red: 180/255, green: 180/255, blue: 180/255, alpha: 0.4).cgColor 
           shapeLayer.strokeColor = UIColor(red: 180/255, green: 180/255, blue: 180/255, alpha: 0.4).cgColor 
           shapeLayer.lineWidth = 0.0 

           view!.layer.addSublayer(shapeLayer) 
          } 

         } 
        } 
+0

好吧,看起來你正在混合2種不同的技術在這裏,你讓自己感到困惑。你不想處理核心圖形, – Knight0fDragon

回答

1

看你的代碼,我相信代碼的元兇行是:

circlePath.fill() 

由於UIBezierPath.fill()是核芯顯卡繪製操作,它需要一個核芯顯卡內部調用,以便它知道它將在哪裏實際繪製。

這通常是內部UIGraphicsBeginImageContextWithOptions()/UIGraphicsEndImageContext()做,你明確地創建和結束背景下,或在某些UIKit的方法,比如UIView.drawRect()文意爲你自動管理。

從它的外觀來看,在代碼的那部分中調用fill()是在出現的上下文之外完成的,這就是爲什麼它報告0x0的無效上下文。

在這種特殊情況下,您看起來好像使用circlePath對象作爲CAShapeLayer的剪貼蒙版,因此可能不需要在那裏調用fill()

讓我知道你是否需要額外的說明。 :)

相關問題