Loading an image in swift with several options

I am trying to upload an image to the backend client using swift. The problem is that I cannot get the correct formatting for httpbody. I do not want to use a multi-page form for uploading, because I do not know how to handle it on the server.

Here is the code that I have .. it doesn’t work when I view the image online, which it doesn’t display, and it’s only like 70 kb, which I know is definitely not like a big image.

var bodyString: String = "session_id=\(session_id)&location_id=\(location_id)" bodyString = bodyString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! var body = NSMutableData.alloc() body.appendData(bodyString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!) if image != nil{ var imageData = UIImageJPEGRepresentation(image,0.5) body = NSMutableData.alloc() //var imageDataString = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) bodyString = "session_id=\(session_id)&location_id=\(location_id)&image_data=" bodyString = bodyString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! body.appendData(bodyString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!) var imageString = "&image_data=\(imageData)" body.appendData(imageData) } req.HTTPBody = body 

UPDATE: so I decided to go for the base64 route, but it still doesn't work, I think because I encode it as ntf8string, is this the right way to do this?

 var imageData = UIImageJPEGRepresentation(image,0.5) var imageDataString = imageData.base64EncodedStringWithOptions(.allZeros) body = NSMutableData.alloc() bodyString = "session_id=\(session_id)&location_id=\(location_id)&tag_type=\(tag_type)&image_data=\(imageDataString)" bodyString = bodyString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! body.appendData(bodyString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!) 

and on the backend I decode it as:

 image_data_decoded = base64.b64decode(image_data) 
+1
ios swift network-programming
May 2, '15 at 19:01
source share
1 answer

You cannot post binary data in an application/x-www-form-urlencoded request like this. Actually, the code in your question looks like it is trying to send a hexadecimal string representation of binary data, which is probably not what you intended, and even if you intended to do it, (a) you will have to decode it somehow server side (b) note that this is very inefficient (more than double the size of the image payload): and (c) there would have to be a percentage that escaped in the request. But I don’t think that you would like for everyone, anyway, so probably everything is debatable.

You can usually either create a multipart/form-data request, as described here (in which the downloaded file is included in a file, for example $_FILES in PHP), or one of them converts this binary data to text (for example, using base64), and the code The server converts the base64 value for the image_data key back to binary data.

By the way, I could suggest Alamofire or AFNetworking as an alternative to trying to correctly create queries. It does not change the main problem (you need to choose between base64 encoding or multipart requests), but it simplifies Swift code.

0
May 2 '15 at 20:57
source share



All Articles