【问题标题】:UnsafeRawPointer MigrationUnsafeRawPointer 迁移
【发布时间】:2017-03-30 18:39:40
【问题描述】:

我在 GitHub 上找到了一个不错的 360 播放器项目。 https://github.com/iosdevzone/360Video

当我将它转换到最后一个 Swift3 语法时,我得到了这个 error

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

'UnsafeMutablePointer' 的重载存在这些部分匹配的参数列表:(RawPointer), (OpaquePointer), (OpaquePointer?), (UnsafeMutablePointer), (UnsafeMutablePointer?)"

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

它发生在这段代码中:

// MARK: - 纹理

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

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

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

    let imageColorSpace = image.cgImage?.colorSpace
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
    let gc = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 4 * width, space: imageColorSpace, bitmapInfo: bitmapInfo.rawValue)
    gc.draw(image.cgImage, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height)))

    self.updateTexture(CGSize(width: width, height: height), imageData: imageData)
    free(imageData)
}

【问题讨论】:

    标签: ios xcode migration swift3


    【解决方案1】:

    这段代码:

    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>?
    

    (未测试)

    【讨论】:

    • 谢谢!所以会有 ptrCount 而不是 imageData
    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    相关资源
    最近更新 更多