Is it possible to pass a ByteArray from Flash to C++ and back. We are using the Obj-C PlayerIO library for multiplayer. Their plugin takes and gives NSData that is supposed to represent a ByteArray, but I am not sure how to convert the values. On making the AS call, the Obj Value shows as VT_Object. On receiving NSData from the server, is it possible to convert this back to VT_Object type to send back to AS?
Great question, could you figure it out? It could solve my issue too if there's a possibility to do this and then create a bitmap from that byteArray with flash.
It seems that no one from the dev team reads our questions lol
Cheers
Pedro
If you are just looking to get an image from Obj-C to Flash, we handle that by passing it as a base64 string. Here's some example code that we use to asynchronously load an image from a URL into Flash.
//============================================================================// // C++ Handler void F84UtilitiesHandler::Call(const Params& params) { int method = int(params.pUserData); switch(method) { case METHOD_LoadImageFromURL: LoadImageFromURL(params.pArgs[0].GetString(), params.pArgs[1]); break; } } //============================================================================// // C++ Handler void F84UtilitiesHandler::LoadImageFromURL(const char* url, GFx::Value& success) { // In order to save C++ objects in the block below __block GFx::Value successFunc = success; [utilitiesManager loadImageFromURL:[NSString stringWithUTF8String:url] success:^(NSString* base64Image) { GFx::Value args[1]; args[0].SetString([base64Image UTF8String]); successFunc.InvokeSelf(NULL, args, 1); }]; } //============================================================================// // Obj-C Class - (void)loadImageFromURL:(NSString*)url success:(void(^)(NSString* base64Image))success { NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError* error) { NSString* base64Image = [data base64Encoding]; success(base64Image); }]; } //============================================================================// // Actionscript public function loadAvatarFromURL(url:String):void { F84UtilitiesInterface.instance.loadImageFromURL(url, function(base64Image:String):void { var byteArray:ByteArray = Base64.decodeToByteArray(string); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.INIT, function(e:Event):void { var bitmapData:BitmapData = new BitmapData(loader.width, loader.height); bitmapData.draw(loader); var bitmap:Bitmap = new Bitmap(bitmapData); bitmap.width = AVATAR_WIDTH; bitmap.height = AVATAR_HEIGHT; avatarSpaceMovieClip.addChild(bitmap); }); loader.loadBytes(byteArray); } } //============================================================================//
I'm working on Android, but the flow would be to to get the image in C++ and base 64 encode those bytes in a string variable and pass it to Actionscript, then in AS base64 decode them to bytes and create the image? Am I right? Any known issues with some of this flow in Android?
You are being very helpful, thank you!
I haven't written the Android plugin for this yet, but the flow should be the same. I think the bytes you are encoding must be either in JPEG or PNG format before you convert them to base64. I would take a guess that this would be the correct way on Android to get the base64 string:
http://stackoverflow.com/questions/17191301/android-image-to-base64-string-issue
I will be trying to implement this tonight or tomorrow, I really appreciate your help, huge thanks
I ended up just handling this with strings, but I just found out that you can read and write to ByteArrays in C++ with ReadFromByteArray and WriteToByteArray
GFx::Value byteArrayValue = params.pArgs[0];
int byteArraySize = byteArrayValue.GetByteArraySize(); UByte* byteArray = (UByte*)malloc(byteArraySize * sizeof(UByte)); byteArrayValue.ReadFromByteArray(byteArray, byteArraySize); NSData* data = [NSData dataWithBytes:byteArray length:byteArraySize]; free(byteArray);
Guess I'll have to go and rewrite that whole system