如何访问由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