gpt4 book ai didi

ios - 如何在 iOS 上的 Swift 中保存和恢复图形上下文?

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:39 24 4
gpt4 key购买 nike

我正在将一个 Mac 应用程序移植到 iOS(以及从 Objective-C 到 Swift),在 Xcode 中,我在控制台中收到几个错误,指出我正在使用无效的图形上下文:

: CGContextSetFillColorWithColor: invalid context 0x0.

: CGContextSetStrokeColorWithColor: invalid context 0x0.

: CGContextGetCompositeOperation: invalid context 0x0.

: CGContextSetCompositeOperation: invalid context 0x0.

: CGContextFillRects: invalid context 0x0.

但是,即使在删除了我进入 Core Graphics 的代码并且仅使用 UIColor 和 UIBezierPath 之后,错误仍然存​​在。

我直接使用 Core Graphics 的唯一代码是设置阴影,但我强烈怀疑我在 Swift 中保存和恢复图形上下文的代码是错误的:

if let context: CGContext! = UIGraphicsGetCurrentContext() {
CGContextSaveGState(context)
let shadowColor = UIColor(white:0, alpha:0.75).CGColor
CGContextSetShadowWithColor(context, offset, blurRadius, shadowColor)
}

// Do drawing here...

if let context: CGContext! = UIGraphicsGetCurrentContext() {
CGContextSetShadowWithColor(context, CGSizeMake(0.0, 0.0), CGFloat(0.0), nil)
CGContextRestoreGState(context)
}

此处是否会使用两个不同的上下文值?这是这段代码的问题吗?感谢您的帮助。

最佳答案

这是我在 Swift 中用来保存和恢复上下文的内容:

// Save the graphics context
let currentContext = UIGraphicsGetCurrentContext()
CGContextSaveGState(currentContext)

... your graphics operations here ...

// Restore the previously saved context
CGContextRestoreGState(currentContext)

关于ios - 如何在 iOS 上的 Swift 中保存和恢复图形上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24167922/

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