How to get bytes out of an UnsafeMutableRawPointer?

后端 未结 4 1160
孤独总比滥情好
孤独总比滥情好 2020-12-02 21:05

How does one access bytes (or Int16\'s, floats, etc.) out of memory pointed to by an UnsafeMutableRawPointer (new in Swift 3) handed to a Swift function by a C API (Core Aud

4条回答
  •  旧时难觅i
    2020-12-02 21:30

    Create Data object.

    init(bytesNoCopy bytes: UnsafeMutableRawPointer, count: Int, deallocator: Data.Deallocator)
    

    One important way missing from the other answers here is initialising a Data object with UnsafeMutableRawPointer. The data object can then be used for other calculations.

    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
    }
    

提交回复
热议问题