How to use UnsafeMutableRawPointer to fill an array?

前端 未结 5 1988
傲寒
傲寒 2021-02-09 02:25

I have a Metal texture, I want to access its data from Swift by making it a float4 array (so that I can access each pixel 4 color components).

I discovered this method

5条回答
  •  灰色年华
    2021-02-09 02:49

    To complement @VasilyBodnarchuk's answer:

    extension UnsafeMutableRawPointer {
        func toArray(to type: T.Type, capacity count: Int) -> [T] {
            return Array(UnsafeBufferPointer(start: bindMemory(to: type, capacity: count), count: count))
        }
    }
    

提交回复
热议问题