Home > Software engineering >  POST NSData to a Server
POST NSData to a Server

Time:12-10

I am trying to POST the iPhone depth data to a Macbook using wifi (at-least 30 fps). I extracted depth CVPixelBuffer from ARSession followed by creating an NSMutableData.

My question is that how I can POST this message (NSMutableData) to a server? In the meantime, how can I GET the same message from the server in Python, preferably Flask, and convert the message to depth data for showing purposes?

I would really appreciate any help and thanks for your time.

func createDataFromPixelBuffer(pixelBuffer: CVPixelBuffer) -> Data {
    CVPixelBufferLockBaseAddress(pixelBuffer, [.readOnly])
    defer { CVPixelBufferUnlockBaseAddress(pixelBuffer, [.readOnly]) }

    let source = CVPixelBufferGetBaseAddress(pixelBuffer)
    let totalSize = CVPixelBufferGetDataSize(pixelBuffer)
    guard let rawFrame = malloc(totalSize) else {fatalError("failed to alloc "   String(totalSize))}
    memcpy(rawFrame, source, totalSize)

    return Data(bytesNoCopy: rawFrame, count: totalSize, deallocator: .free)
}

let data = createDataFromPixelBuffer(pixelBuffer: depth)
let messageData = NSMutableData()
messageData.append(data)

CodePudding user response:

To create the HTTP request, you will need to use a networking library such as URLSession in the iOS SDK.

Here is an example of how you could use URLSession to create a POST request in Swift:

// Create a URL to the server
let url = URL(string: "http://example.com/upload")!

// Create a URLRequest with the POST method and the data to be sent in the HTTP body
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = messageData as Data

// Use URLSession to send the request
let task = URLSession.shared.dataTask(with: request) { data, response, error in
    // Handle the response here
}

task.resume()

To receive the depth data on the server side, you can create a Flask route that listens for POST requests. Here is an example of how you could do that in Python:

from flask import Flask, request

app = Flask(__name__)

@app.route('/upload', methods=['POST'])
def upload():
    # Get the data from the HTTP body
    data = request.get_data()

    # Convert the data to a depth image here

    return "OK"
  • Related