![](/img/trans.png)
[英]How to do corners right with Cocoa Touch (iOS) Graphics Context Drawing?
[英]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.