1

Converting to Swift 3 I get the compiler error:

Cannot invoke initializer for type 'UnsafeMutablePointer' with an argument list of type '(UnsafeMutableRawPointer?)'

when trying to create a typed pointer from an opaque pointer:

let imageRef = myUIImage.cgImage!
let context = CGContext(...)!
let rect = CGRect(...)
context.draw(imageref, in: rect)

var data = context.data
var dataType = UnsafeMutablePointer<UInt8>(data) //Throws the error

Why does this not work?

Manuel
  • 14,274
  • 6
  • 57
  • 130
  • Compare [How to get bytes out of an UnsafeMutableRawPointer?](http://stackoverflow.com/questions/38983277/how-to-get-bytes-out-of-an-unsafemutablerawpointer) – Martin R Sep 16 '16 at 21:06

1 Answers1

0

content.data returns a UnsafeMutableRawPointer. It has a bindMemory and assumingMemoryBound methods to convert to typed pointers:

let dataType = data.bindMemory(to: UInt8.self, capacity: n) // UnsafeMutablePointer<UInt8>

I don't have a chance to test what n is, but I suspect it will be something like this:

n = width * height * bitsPerPixel / 8
Code Different
  • 90,614
  • 16
  • 144
  • 163