gpt4 book ai didi

ios - 将 float 数组的数组转换为 UnsafeMutablePointer>

转载 作者:行者123 更新时间:2023-11-29 05:33:37 25 4
gpt4 key购买 nike

我需要使用一个使用 UnsafeMutablePointer<UnsafeMutablePointer<Float>?>! 的函数作为参数。它是用 Objective-C++ 框架编写的,我通过桥接 header 访问它:

- (void) applyFilters: (float *) input_image input_params: (long *) input_params output_image : (float *) output_image output_params : (long *) output_params filter_id : (int) filter_id args : (float**) args

我在理解 args 参数的结构时遇到了一些困难,即 float** 。在 Swift 中,它要求 UnsafeMutablePointer<UnsafeMutablePointer<Float>> 类型的参数,我猜测它指向一个 float 组的数组。

applyFilters(input_image: UnsafeMutablePointer<Float>!UnsafeMutablePointer<Float>!, input_params: UnsafeMutablePointer<Int>!, output_image: UnsafeMutablePointer<Float>!, output_params: UnsafeMutablePointer<Int>!, filter_id: Int32, args: UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!)

如果我创建一个 float 数组的数组,如何使用 UnsafeMutablePointer<UnsafeMutablePointer<Float>> 指向它所以我可以将它传递给 C++ 函数?

最佳答案

弄清楚如何做到这一点:

func arrayOfArrayExample() {

let floatArray: [[Float]] = [
[50.0, 23.0, 15.6],
[24.0, 9.5, 12.7]
]

let size1 = floatArray.count
let size2 = floatArray[0].count

var vectorBuf : UnsafeMutablePointer<Float>?
let bufferOut = UnsafeMutablePointer< UnsafeMutablePointer<Float>?>.allocate(capacity: Int(size1))

for i in 0..<size1 {
vectorBuf = UnsafeMutablePointer<Float>.allocate(capacity: size2)
for j in 0..<size2 {
vectorBuf!.advanced(by: j).pointee = floatArray[i][j]
}
bufferOut.advanced(by: i).pointee = vectorBuf
}

defer {
//deallocating memory
for i in 0..<size1 {
bufferOut.advanced(by: i).deallocate()
}
}
}

变量bufferOut返回 UnsafeMutablePointer<UnsafeMutablePointer<Float>>指向 Float 数组的数组的指针,随后释放内存。

关于ios - 将 float 数组的数组转换为 UnsafeMutablePointer<UnsafeMutablePointer<Float>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com