1

转换为 Swift 3 我得到编译器错误:

无法使用类型为“(UnsafeMutableRawPointer?)”的参数列表调用类型“UnsafeMutablePointer”的初始化程序

尝试从不透明指针创建类型化指针时:

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

为什么这不起作用?

4

1 回答 1

0

content.data返回一个UnsafeMutableRawPointer。它具有转换为类型化指针的bindMemory和方法:assumingMemoryBound

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

我没有机会测试是什么n,但我怀疑它会是这样的:

n = width * height * bitsPerPixel / 8
于 2016-09-16T21:05:26.467 回答