如何訪問由UnsafeMutableRawPointer(Swift 3中新增)所指向的內存中的字節(或Int16的,浮點等)由C API交付Swift函數(Core Audio等)如何從UnsafeMutableRawPointer中獲取字節?
18
A
回答
34
load<T>
從存儲器讀取原始字節,並構造T
類型的值:在一個字節偏移
let ptr = ... // Unsafe[Mutable]RawPointer
let i16 = ptr.load(as: UInt16.self)
任選:
let i16 = ptr.load(fromByteOffset: 4, as: UInt16.self)
還有assumingMemoryBound()
其中從01轉換到Unsafe[Mutable]Pointer<T>
,假設被指向的存儲器包含類型T的值:
let i16 = ptr.assumingMemoryBound(to: UInt16.self).pointee
對於值的陣列可以創建一個「緩衝器指針」:
let i16bufptr = UnsafeBufferPointer(start: ptr.assumingMemoryBound(to: UInt16.self), count: count)
緩衝器指針可能已經足夠用於您的目的,它 是可下標的,並且可以類似於數組枚舉。 如有必要,創建從緩衝器指針的數組:
let i16array = Array(i16bufptr)
正如@Hamish所述,更多的信息和細節可以在
1
下面是api文檔不安全[Mutable] RawPointer to T/Unsafe [MutablePointer]轉換:
/// Binds the allocated memory to type `T` and returns an
/// `UnsafePointer<T>` to the bound memory at `self`.
///
/// - Precondition: The memory is uninitialized.
/// - Postcondition: The memory is bound to 'T' starting at `self` continuing
/// through `self` + `count` * `MemoryLayout<T>.stride`
/// - Warning: Binding memory to a type is potentially undefined if the
/// memory is ever accessed as an unrelated type.
public func bindMemory<T>(to type: T.Type, capacity count: Int) -> UnsafePointer<T>
/// Converts from an `UnsafeRawPointer` to UnsafePointer<T> given that
/// the region of memory starting at `self` is already bound to type `T`.
///
/// - Precondition: The memory is bound to 'T' starting at `self` for some
/// unspecified capacity.
///
/// - Warning: Accessing memory via the returned pointer is undefined if the
/// if the memory has not been bound to `T`.
public func assumingMemoryBound<T>(to: T.Type) -> UnsafePointer<T>
/// Reads raw bytes from memory at `self + offset` and constructs a
/// value of type `T`.
///
/// - Precondition: The underlying pointer plus `offset` is properly
/// aligned for accessing `T`.
///
/// - Precondition: The memory is initialized to a value of some type, `U`,
/// such that `T` is layout compatible with `U`.
public func load<T>(fromByteOffset offset: Int = default, as type: T.Type) -> T
,然後從Unsafe[MutablePointer]<T>
到T
可以與 pointee
和move
的API被轉換
/// Accesses the `Pointee` instance referenced by `self`.
///
/// - Precondition: the pointee has been initialized with an instance of
/// type `Pointee`.
public var pointee: Pointee { get }
/// Retrieves the `pointee`, returning the referenced memory to an
/// uninitialized state.
///
/// Equivalent to `{ defer { deinitialize() }; return pointee }()`, but
/// more efficient.
///
/// - Precondition: The pointee is initialized.
///
/// - Postcondition: The memory is uninitialized.
public func move() -> Pointee
3
創建數據對象。從其他的答案在這裏失蹤
init(bytesNoCopy bytes: UnsafeMutableRawPointer, count: Int, deallocator: Data.Deallocator)
一個重要方式是與初始化一個UnsafeMutableRawPointer數據對象。數據對象可以用於其他計算。
public func base64(quality: Int32 = 67) -> String? {
var size: Int32 = 0
if let image = gdImageJpegPtr(internalImage, &size, quality) {
// gdImageJpegPtr returns an UnsafeMutableRawPointer that is converted to a Data object
let d = Data(bytesNoCopy: image, count: Int(size), deallocator: .none)
return d.base64EncodedString()
}
return nil
}
+0
這來得非常方便!我需要將dealloc更改爲none,但這對我來說是個訣竅。非常感謝。 – VaporwareWolf
1
這裏的文字UINT8數組轉換爲UnsafeMutableRawPointer並回到一個UInt32的陣列的雨燕4例如
static func unsafePointerTest() {
//let a : [UInt8] = [0,0,0,4,0,0,0,8,0,0,0,12]
let a : [UInt8] = [0x04, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
0x0C, 0x00, 0x00, 0x00] //little endian
//0xFF, 0xF0, 0xF0, 0x12] //317780223 = 12F0F0FF
let b:UnsafeMutableRawPointer = UnsafeMutableRawPointer(mutating:a)
let bTypedPtr = b.bindMemory(to: UInt32.self, capacity: a.count/4)
let UInt32Buffer = UnsafeBufferPointer(start: bTypedPtr, count: a.count/4)
let output = Array(UInt32Buffer)
print(output)
}
相關問題
- 1. 如何從字節數組中獲取字節並獲取數字
- 2. 如何從PictureBox獲取字節數組?
- 3. 如何從S3獲取GetObjectResponse的字節?
- 4. 如何從字節獲取位值?
- 5. 如何從JTextArea獲取字節數組?
- 6. 如何從字節數組([UInt8])獲取一個字節(UInt8)?
- 7. 從一個字節獲取半字節
- 8. 如何獲取從QTextStream中讀取的字節數
- 9. 如何從字節字中獲取所選位的位置值
- 10. Bash:如何從一個數字中獲取一個字節?
- 11. 如何從查詢字符串中獲取原始字節
- 12. 如何從字符串中適當地獲取字節?
- 13. 如何從Java字符串中獲取正好16個字節
- 14. 從JSON獲取字節
- 15. 從Cloudinary URL獲取字節[]
- 16. 從IBUFFER獲取字節
- 17. ECDiffieHellmanCng獲取ECDiffieHellmanPublicKey從字節[]
- 18. 從字節獲取位圖[]
- 19. 從SqlDataReader獲取字節[]?
- 20. 如何從一節中獲取ActivityNode?
- 21. 如何從UITable SectionHeaderView中獲取節號?
- 22. 如何從本地文件中獲取字節[],例如/sdcard/tets.png?
- 23. 從java中的多字節變量中獲取單個字節
- 24. 如何從InputStream中讀取字節?
- 25. 如何從文件中讀取字節
- 26. 如何從字節中提取MimeType []
- 27. 獲取字節[]從在C++中的char *
- 28. 從URL中獲取前n個字節
- 29. 從幀中獲取幀值字節
- 30. 如何從我寫入的流中獲取字節
能否請您給我們一個[最小的,完整的和可驗證的示例](HTTP ://stackoverflow.com/help/mcve)的一些具體的數據工作? – Alexander
[不安全路由指針的Swift進化提議](https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md)可能是一個很好的開始 - 提供一個很好的概述用API舉例說明。 – Hamish