首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在bezierPath上绘制CAShapeLayer时上下文无效

在bezierPath上绘制CAShapeLayer时上下文无效
EN

Stack Overflow用户
提问于 2019-09-27 07:57:40
回答 2查看 145关注 0票数 1

因此,在运行我的代码时,我会得到以下错误:

代码语言:javascript
运行
复制
CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

当我设置CG_CONTEXT_SHOW_BACKTRACE环境变量时,错误指向我的func bezierPath(dentInMM value: Float) -> CGPath函数。下面是组成控制器这一部分的代码片段:

代码语言:javascript
运行
复制
var shape = CAShapeLayer()

func someFunc() {
  self.dentView.layer.addSublayer(self.shape)

  let color: UIColor = .systemGreen
  self.shape.fillColor = color.withAlphaComponent(0.3).cgColor
  self.shape.strokeColor = color.cgColor
  self.shape.lineWidth = 6
  self.shape.path = self.bezierPath(dentInMM: 0)
}

func bezierPath(dentInMM value: Float) -> CGPath {
    self.shape.frame = self.dentView.bounds

    let x000 = Int(self.shape.frame.minX)
    let x050 = Int(self.shape.frame.midX)
    let x100 = Int(self.shape.frame.maxX)
    let x010 = x100 / 10
    let x040 = x050 - x010
    let x060 = x050 + x010
    let yOffSet = Int(self.shape.frame.minY)

    let indentation = Int(value * self.dentDisplayMultiplier)

    let bezierPath = UIBezierPath()
    bezierPath.move(to: CGPoint(x: x000, y: yOffSet))
    bezierPath.addCurve(to: CGPoint(x: x050, y: indentation), controlPoint1: CGPoint(x: x050, y: yOffSet), controlPoint2: CGPoint(x: x040, y: indentation))
    bezierPath.addCurve(to: CGPoint(x: x100, y: yOffSet), controlPoint1: CGPoint(x: x060, y: indentation), controlPoint2: CGPoint(x: x050, y: yOffSet))
    UIColor.label.setStroke()
    bezierPath.stroke()

    return bezierPath.cgPath
}

我需要在某个地方指定上下文吗?我的印象是CAShapeLayer提供了.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-27 09:16:17

不必调用绘图方法

bezierPath.stroke()

CAShapeLayer将在dentView中绘制自己

你真的需要

UIColor.label.setStroke()

因为您已经在someFunc()中设置了someFunc()

以下是没有错误的工作原理:

代码语言:javascript
运行
复制
class ThirdViewController: UIViewController {
    let dentDisplayMultiplier : Float = 6
    var shape = CAShapeLayer()

    @IBOutlet weak var dentView: UIView!


    func someFunc() {
      self.dentView.layer.addSublayer(self.shape)

      let color: UIColor = .systemGreen
      self.shape.fillColor = color.withAlphaComponent(0.3).cgColor
      self.shape.strokeColor = color.cgColor
      self.shape.lineWidth = 6
      self.shape.path = self.bezierPath(dentInMM: 3)
    }

    func bezierPath(dentInMM value: Float) -> CGPath {
        self.shape.frame = self.dentView.bounds

        let x000 = Int(self.shape.frame.minX)
        let x050 = Int(self.shape.frame.midX)
        let x100 = Int(self.shape.frame.maxX)
        let x010 = x100 / 10
        let x040 = x050 - x010
        let x060 = x050 + x010
        let yOffSet = Int(self.shape.frame.minY)

        let indentation = Int(value * self.dentDisplayMultiplier)

        let bezierPath = UIBezierPath()
        bezierPath.move(to: CGPoint(x: x000, y: yOffSet))
        bezierPath.addCurve(to: CGPoint(x: x050, y: indentation), controlPoint1: CGPoint(x: x050, y: yOffSet), controlPoint2: CGPoint(x: x040, y: indentation))
        bezierPath.addCurve(to: CGPoint(x: x100, y: yOffSet), controlPoint1: CGPoint(x: x060, y: indentation), controlPoint2: CGPoint(x: x050, y: yOffSet))

        return bezierPath.cgPath
    }

    override func viewDidLoad() {


        super.viewDidLoad()
        // Do any additional setup after loading the view.
        someFunc()

    }

}
票数 4
EN

Stack Overflow用户

发布于 2019-09-27 08:27:15

尝尝这个

代码语言:javascript
运行
复制
let bezierPath = UIBezierPath()

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

bezierPath.move(to: CGPoint(x: x000, y: yOffSet))
bezierPath.addCurve(to: CGPoint(x: x050, y: indentation), controlPoint1: CGPoint(x: x050, y: yOffSet), controlPoint2: CGPoint(x: x040, y: indentation))
bezierPath.addCurve(to: CGPoint(x: x100, y: yOffSet), controlPoint1: CGPoint(x: x060, y: indentation), controlPoint2: CGPoint(x: x050, y: yOffSet))
UIColor.label.setStroke()
bezierPath.stroke()

UIGraphicsEndImageContext()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58130232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档