繁体   English   中英

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

[英]How do I save and restore a graphics context in Swift on iOS?

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

:CGContextSetFillColorWithColor:无效的上下文0x0。

:CGContextSetStrokeColorWithColor:无效的上下文0x0。

:CGContextGetCompositeOperation:无效的上下文0x0。

:CGContextSetCompositeOperation:无效的上下文0x0。

:CGContextFillRects:无效的上下文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)

更新了Swift-3代码以保存和恢复图形上下文 : -

//To save current Context
    let context = UIGraphicsGetCurrentContext()
    context!.saveGState()

//To restore 
    context!.restoreGState()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM