Question

I think I need some assistance in figuring out the correct NSJSONSerialization option to make my problem go away.

On my app I allow the user to select an image from the gallery - the image undergoes the following:

NSData *imageData = UIImageJPEGRepresentation(self.profileImageView.image, 0.0);

then

NSString *stringOfImageData = [imageData base64EncodedStringWithOptions:0];

before it is serialized like this:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];

and then sent to my REST API. I then decode it in python using base64 like so:

profileImageData = base64.b64decode(request.json['image'])

It is then loaded in GridFS (mongodb). On extracting the data to send back to the app I first encode in base to base64 before using dumps() to send it back:

dumps(base64.b64encode(fs.get_last_version(request.json['userID']).read()))

Within iOS after receiving the data it goes through the below de-serialization:

[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error]

I have narrowed by problem to the last NSJSONSerialization command. After the data is received by the app it is able to print to screen. After the Serialization I get a 'nil' :(

The Serialization and De-Serialization has been working great for strings, integers etc - it just doesn't work when I'm trying to move image data.

Thanks

EDIT: I am able to run a curl request against the API and then using an online base64 to image converter I can see my image. So it definitely means the issues is with the iOS side of decoding a json encoded base64 string.

EDIT: When I repeatedly run the deserialization - every 20th time or so the data is correctly converted. I think the solution might have to be to break up the data coming in.

EDIT: Error:

parsed error:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unterminated string around character 17.) UserInfo=0x109c08790 {NSDebugDescription=Unterminated string around character 17.}
Was it helpful?

Solution

What you don't say is how you are receiving the data. My guess is you are trying to decode the data before you receive all of it, but since I don't know how it's a guess.

To better understand what's going on, try logging the size and hash of the data, to see if the length varies. You can also save each received data object to the file system - put them in the Documents folder and you can access them from your Mac. If the size never varies you will then have to compare a good data object to a bad one.

In fact you can write a little code to save an image as data and a base64 string, upload it, then pull it back, and save it. Now compare the data and strings. Once you find a difference, then look at. What is its offset from the start? How is it different?

When you understand this all you will be able to fix it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top