In draw(_:) the UIGraphicsGetCurrentContext no longer return CGBitmapContext

In the ios12 beta, our app crashes. We need to access some attributes from the current context, below are some codes:


public override func draw(_ frame: CGRect) {

let context = UIGraphicsGetCurrentContext()!

let imgData = unsafeBitCast(context.data, to:UnsafeMutablePointer<CUnsignedChar>.self)

let bitmapWidth = context.bytesPerRow * 8 / context.bitsPerPixel

}



It crashes because context is not type CGBitmapContext, it's kCGContextTypeUnknown. So all the methods for bitmap return 0 or nil. It's quite confusing, all the previous iOS version works properly. I haven't found any document on this change so far. Can anyone help?

Replies

Did you ever find a solution to this?

I had a similar problem. You can't assume that the context returned is a bitmap context, so you'll have to do something else. Can you get the width or height from the view instead?

Are there any options for getting the whole context from UIGraphicsGetCurrentContext()...

I'm drawing lines and need the context to draw...

upgrading to a new OS should not trash items like this, or at least ways around it should be provided.