在 iOS/Swift 中使用 withMemoryRebound 时出现致命错误
Fatal error when using withMemoryRebound in iOS/Swift
我有以下代码创建一个 table 用于使用 Swift 加速函数
在 iOS 中对图像进行采样
当我将内存重新绑定到 table 创建期望来自原始类型 Int 的 UInt16 时,我得到了一个致命错误。
var arr = Array<Float>(repeating: 0, count: 163840)
arr.withUnsafeBufferPointer{
arr_pointer in do {
arr_pointer.withMemoryRebound(to: UInt16.self){ // This causes a FATAL ERROR
arr_r_pointer in do {
let table = vImageMultidimensionalTable_Create( arr_r_pointer.baseAddress!,
3, 3, dims_r_pointer.baseAddress!, kvImageMDTableHint_Float,
vImage_Flags(kvImageNoFlags), nil )
vImageMultiDimensionalInterpolatedLookupTable_PlanarF( &srcBuffer,
&destBuffer,nil,table!,
kvImageFullInterpolation,
vImage_Flags(kvImageNoFlags))
}
}
}
}
有人能指出我的错误吗?
你原来的数组arr
是Float
s
的数组
var arr = Array<Float>(repeating: 0, count: 163840)
但您正试图将指针绑定到 UInt16
arr_pointer.withMemoryRebound(to: UInt16.self)
您应该阅读 Note
以了解 withMemoryRebound
功能:
Note
Only use this method to rebind the buffer’s memory to a type with the same size and stride as the currently bound Element
type. To
bind a region of memory to a type that is a different size, convert
the buffer to a raw buffer and use the bindMemory(to:)
method.
Float
的大小是32位,UInt16
的大小是16位,所以他们不'尺寸相同不能反弹
所以你应该这样做:
arr.withUnsafeBufferPointer { pointer in
let raw = UnsafeRawBufferPointer(pointer)
let uints = raw.bindMemory(to: UInt16.self)
// use buffer pointer to `UInt16`s here
}
但还要注意,初始数组中的每个 Float
将以这种方式分成两个 UInt16
。不知道是不是你需要的
我有以下代码创建一个 table 用于使用 Swift 加速函数
在 iOS 中对图像进行采样当我将内存重新绑定到 table 创建期望来自原始类型 Int 的 UInt16 时,我得到了一个致命错误。
var arr = Array<Float>(repeating: 0, count: 163840)
arr.withUnsafeBufferPointer{
arr_pointer in do {
arr_pointer.withMemoryRebound(to: UInt16.self){ // This causes a FATAL ERROR
arr_r_pointer in do {
let table = vImageMultidimensionalTable_Create( arr_r_pointer.baseAddress!,
3, 3, dims_r_pointer.baseAddress!, kvImageMDTableHint_Float,
vImage_Flags(kvImageNoFlags), nil )
vImageMultiDimensionalInterpolatedLookupTable_PlanarF( &srcBuffer,
&destBuffer,nil,table!,
kvImageFullInterpolation,
vImage_Flags(kvImageNoFlags))
}
}
}
}
有人能指出我的错误吗?
你原来的数组arr
是Float
s
var arr = Array<Float>(repeating: 0, count: 163840)
但您正试图将指针绑定到 UInt16
arr_pointer.withMemoryRebound(to: UInt16.self)
您应该阅读 Note
以了解 withMemoryRebound
功能:
Note
Only use this method to rebind the buffer’s memory to a type with the same size and stride as the currently bound
Element
type. To bind a region of memory to a type that is a different size, convert the buffer to a raw buffer and use thebindMemory(to:)
method.
Float
的大小是32位,UInt16
的大小是16位,所以他们不'尺寸相同不能反弹
所以你应该这样做:
arr.withUnsafeBufferPointer { pointer in
let raw = UnsafeRawBufferPointer(pointer)
let uints = raw.bindMemory(to: UInt16.self)
// use buffer pointer to `UInt16`s here
}
但还要注意,初始数组中的每个 Float
将以这种方式分成两个 UInt16
。不知道是不是你需要的