The title may be a bit confusing, and then describe the problem I have:
I am responsible for making an iOS version of the app, and the server and Android versions are all developed. The image of the server is not the path, but the Blob stream object in the database, because of the requirements so the data are used JSON value, the image server side of the processing method is to convert the picture binary stream into a string. Server and Android are Java development, very convenient, directly with the Jackson package and parsing is OK, and the iOS side said dead can not parse this string into a picture!
I do not know if you see this can directly find out where the problem is, anyway I was tortured by this problem for nearly two days.
Idea 1
At first I thought that this problem should not be a problem, it is easy to fix, is to convert the string into NSData object, and then use the UIImage Initwithdata method.
NSData* imgdata = [imgstring datausingencoding:nsutf8stringencoding]; After writing the code, The first feeling with UTF8 analytic picture flow a bit strange, sure enough, the picture shows not come out.
Idea 2
Since UTF8 not, the default character encoding in Java is not iso-8859-1, change the code!! Not good to make! Sick disorderly Touyi, replaced by ASC2 code, or display not out of the picture!
Idea 3
The consequences are serious! Start complaining, iOS how so pit Dad ah, the string into a byte array how is this wonderful, why do you have to add a character code? I write an analytic algorithm without character encoding!
So I began to write the algorithm, because the JSON passed the picture string is a only English letter and some ordinary symbols of the string, so decided to traverse each character of the string, each character into a 8-bit byte.
Haha, I have to get the byte array to do so, how to repair!! Still not good to make!!! But the idea is finally right.
Idea 4
Learn from the bitter, determined to think about it, the problem in the end where. Don't worry, take your time, believe where the data comes from, and ask what's on Android.
I asked the server and Android docking buddies to get the relevant code, the server executes SQL with resultset directly to the Blob object into the Byte[],bean object "person" class of the Avatar property is directly byte[] type.
Then they directly use the Jackson component to convert the person object into a JSON string, which is then returned directly to the client, Android decryption and then using the same set of components to convert the JSON string into a person object is done!
The question is, how did Jackson's bottom-ground transform?
Final Solution
Because of the time problem, did not study the Jackson bottom code in detail, had to let the server to change the code, the byte array is converted directly to 16 binary string, although the efficiency may be low, but simple and clear. The client only has to parse the 16 binary string. Have time to study Jackson's source code, to see how others have converted.
Experience
Look back at the fact that the problem is very good to solve, is sometimes too much love, think about the ins and outs of the problem, and soon be able to take care of it!
Binary stream of images accepted in iOS with JSON