gpt4 book ai didi

ios - CoreGraphics 绘制结果到 CGContextDrawPath : invalid context 0x0

转载 作者:行者123 更新时间:2023-11-28 16:04:41 24 4
gpt4 key购买 nike

我收到大量 CGContextDrawPath:无效上下文 0x0。如果您想查看回溯,请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量。 此代码中的错误。它在 SKScene 中执行。基本上,我将用户的绘图作为 SKShapeNode 显示在屏幕上。然后我通过 Core Graphics 用圆圈填充该绘图的路径。但是代码运行缓慢并且给我带来了很多错误。我对 Swift 有点陌生。你能帮我看看这是怎么回事吗?我怎样才能加快速度?如何为 CoreGraphics 提供适当的上下文?

                    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)
}

}
}

最佳答案

看看你的代码,我相信代码的罪魁祸首是:

circlePath.fill()

由于 UIBezierPath.fill() 是 Core Graphics 绘制操作,因此需要在 Core Graphics 上下文中调用它,以便它知道实际绘制的位置。

这通常在 UIGraphicsBeginImageContextWithOptions() 内部完成/UIGraphicsEndImageContext()在您显式创建和结束上下文的地方,或者在某些 UIKit 方法(例如 UIView.drawRect())中,上下文会自动为您管理。

从外观上看,在代码的那部分调用 fill() 是在存在的上下文之外完成的,这就是它报告无效上下文 0x0 的原因。

在这种特殊情况下,看起来您正在使用 circlePath 对象作为 CAShapeLayer 的剪切蒙版,因此可能没有必要调用 fill() 那里。

如果您需要进一步说明,请告诉我。 :)

关于ios - CoreGraphics 绘制结果到 CGContextDrawPath : invalid context 0x0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40473226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com