UnsafeRawPointer迁移

时间:2016-11-16 14:10:07

标签: ios xcode migration swift3

我在GitHub上找到了一个有360玩家的好项目。 https://github.com/iosdevzone/360Video

当我转换它到最后 Swift3 语法时,我得到了 错误

无法使用类型为'(UnsafeMutableRawPointer!)的参数列表调用类型'UnsafeMutablePointer'的初始值设定项” 和

这些部分匹配的参数列表存在

''UnsafeMutablePointer'的重载:( RawPointer),(OpaquePointer),(OpaquePointer?),(UnsafeMutablePointer),(UnsafeMutablePointer?)

我也得到了这篇关于如何迁移的文章,但是我自己很难修复它。 https://swift.org/migration-guide/se-0107-migrate.html

它出现在这段代码中:

// MARK: - 纹理

}

//有错误!         让imageData = UnsafeMutablePointer(calloc(Int(width!* height!* 4),sizeof(GLubyte)))

func loadTexture(_ image: UIImage?)
{
    guard let image = image else
    {
        return
    }

    let width = image.cgImage?.width
    let height = image.cgImage?.height

1 个答案:

答案 0 :(得分:0)

此代码:

calloc(Int(width! * height! * 4), sizeof(GLubyte))

返回UnsafeMutableRawPointerUnsafeMutablePointer不提供具有此类型参数的初始值设定项。

而不是使用calloc

 let imageData = UnsafeMutablePointer(calloc(Int(width! * height! * 4), sizeof(GLubyte)))

像这样创建缓冲区数组:

let numPixel = Int(width! * height! * 4)
let buffer = [GLubyte](repeating: 0, count: numPixel)
let imageData = UnsafeMutablePointer<GLubyte>(mutating: buffer) as UnsafeMutablePointer<GLubyte>?

(未经测试)