javascript - Unable to make an Image Blob JSON Serializable -
jira ticket created due base64encode failure: https://jira.appcelerator.org/browse/tc-5876
my current cfg: titanium sdk 5.1.2.ga testing on iphone ios 9.1
i'm stuck in problem in project client requires images took on device (using camera) sent webservice , afterwards seen on device using app (both android , ios devices). titanium provides ti.blob object (event.media) after taking picture (which not json serializable) , need somehow send server. server responds json object, blob must somehow json serializable.
i've tried many ways without success:
1 - base64encode blob
var base64blob = ti.utils.base64encode(event.media);
doesn't work, stucks app , throws asl exceeded maximum size error. imagine image large base64encoded.
2 - read blob buffer
var blobstream = ti.stream.createstream({ source: event.media, mode: ti.stream.mode_read }); var buffer = ti.createbuffer({ length: event.media.length }); var bytes = blobstream.read(buffer);
it works have no idea how can transform buffer holding image contents server can return in json object , later transformed image blob again.
the server can't manage ti.blob objects or ti.buffer objects because, first of all, titanium objects , server c# based, , second due ti.blob , ti.buffer aren't json serializable, json return doesn't work.
what need described in imaginary example below:
var imageblob = event.media; var jsonserializableimg = imageblob.tojson(); sendimagetoserver(jsonserializableimg); var imgfromserver = getimagefromserver(); var imageblob = imgfromserver.toblob(); var imgview = createimageview({ image: imageblob });
i hope can me conversion method possible.
thank's
ok,
this think have do. looking @ api, doable.
1: need create object server side hold blob.
public class blobcontainer { public string filename{get;set;} //... (other properties) public byte[] data {get;set;} }
2: convert important information blob binary array , send server.
var blobstream = ti.stream.createstream({ source: myblob, mode: ti.stream.mode_read }); var newbuffer = ti.createbuffer({ length: myblob.length }); var bytes = blobstream.read(newbuffer);
3: send byte data server through ajax requests. mindful of how big array sending. might advantageous split array , combine on other side (might not necessary):
var dataobjects: [ { id: 1, data: [byte_data_part] }, { id: 2, data: [byte_data_part] }... ] $.each(dataobjects, function(i,a) { $.ajax({ url: "bla", data: json.stringify(a), datatype: "json", type: "post", success: function() { //continue\\ }, error: function() {alert("error bro"}) }); });
4: server side each request in little blob container, store in session object or cache object , once have n out of n, piece , store sucker in database.
5: retrieve stuff in reverse order. remember stored byte[] data. may have fuddle , store string because of way ti buffer creates bytes , way c# interprets bytes. best thing trial , error. once have pieces on client.
var newbuffer = ti.stream.read(data, 0, data.length); var newblob = newbuffer.toblob();
Comments
Post a Comment