1

I'm finally trying to port my Flash games to apps, starting with iOS. I'm stuck on the image saving functionality. In swf's, I used jpegencoder to save to the desktop server. It seemed with some research that that code should still work on mobile, so I tried it, but it doesn't seem to do anything when testing on an iPhone from Adobe AIR (the button registers a click but there is no image that can be found, no notification of anything happening, no asking for permission to access images etc). This is the old code:

saveToDisc.addEventListener(MouseEvent.CLICK, saveToDiscSave);
function saveToDiscSave(e:MouseEvent):void {
    this.visible = false;  //hides menu

    var bitmapData:BitmapData = new BitmapData(1242,1864);
    bitmapData.draw(MovieClip(root));
    var jpgEncoder:JPGEncoder = new JPGEncoder(80);
    var byteArray:ByteArray = jpgEncoder.encode(bitmapData);
    var fileReference:FileReference=new FileReference();
    fileReference.save(byteArray, "Dinogeddon-DollDivine.jpg");

    //this.visible = true;  //returns menu. commented out to see if click registered
}

So with more research, I found the CameraRoll code which looked simple and like it's made specifically for iOS. So I tried that but I'm having the same issue: no evidence that anything's happening. No image saved, no errors, no permission requests.

saveToDisc.addEventListener(MouseEvent.CLICK, saveToDiscSave);
function saveToDiscSave(e:MouseEvent):void {
    this.visible = false;

    var cameraRoll:CameraRoll = new CameraRoll();
    var bitmapData:BitmapData = new BitmapData(1242,1864);
    bitmapData.draw(MovieClip(root));
    cameraRoll.addBitmapData(bitmapData);

    //this.visible = true;
}

Like, I don't even know where I'm going wrong.. Are apps being in testing mode even able to save images? Should I be expecting a notification of some sort to pop up? Is there a size limit on things being saved to a phone? HALP

ola.rogula
  • 307
  • 1
  • 9
  • Do you want the image in the Camera roll? or just stored somewhere on the device where you can retrieve it again later? FileReference I don't think works on mobile, you'd have to use FileStream and File classes to save to the application storage directory. Did you make sure `CameraRoll.supportsAddBitmapData` is true? – BadFeelingAboutThis Apr 08 '16 at 18:15
  • Also, you should listen for errors as the camera roll addBitmapData method is an asynchronous operation. `cameraRoll.addEventListener(ErrorEvent.ERROR, handleMyError);`, you can also listen for the `Event.COMPLETE` event. – BadFeelingAboutThis Apr 08 '16 at 18:24
  • The application will not need the image. It's just for the users' own perusal. I don't care if it's the camera roll specifically.. I just want it saved wherever is customary for this sort of thing? – ola.rogula Apr 08 '16 at 18:37
  • Camera Roll is the most appropriate place then. Listen for errors and check to make sure CameraRoll is supported (should always be on iOS though). – BadFeelingAboutThis Apr 08 '16 at 18:38
  • How do I listen for errors when testing on a device? I would try it out on the emulator on my computer, but it tests the game mega gigantic and cuts the bottom half off on my monitor, so I can't really test most of the buttons that way. It's very frustrating :/ – ola.rogula Apr 08 '16 at 18:38
  • If don't want to setup remote debugging on the device, just put a text field in your application and write the error message out to it. You can debug on the device, I think in FlashPro/AnimateCC it's setup in the AIR settings (click the wrench beside the output target in the publish settings) – BadFeelingAboutThis Apr 08 '16 at 18:39
  • @ola.rogula why cant you debug on device? compile with ipa-debug-interpreter and then fdb to listen. You should have it to test on real device. – Sameer Kumar Jain Apr 08 '16 at 18:45
  • It's not that I don't want to.. It's my first time making an app and I don't even know what's possible. So it is possible to get error messages when testing on the phone? Cool. I don't know how to send error messages to a text field either.. I didn't even know I can control error messages with code hehe. Sounds like it'll be easier to find the setting for it though, I'll try that. But does the cameraRoll code look like it *should* theoretically work? Or am I missing a paragraph about making it write to the disc or something? – ola.rogula Apr 08 '16 at 20:00
  • @ola.rogula in theory the code is correct and should work. Read more about here http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/CameraRoll.html you can add a complete event to check if the addbitmapdata call went correct – Sameer Kumar Jain Apr 08 '16 at 20:10
  • @SameerJain ah thanks, that's perfect! No idea why that documentation didn't come up when I was googling. I just used their sample code and it worked perfect.. asked for permission and everything. I didn't even get a chance to try and turn on error messages. I guess I'll tinker with the code and post my final one as an answer... – ola.rogula Apr 08 '16 at 20:40
  • ..but weird, I was using the code for letting the user browse an image.. but it let them save one instead, enough though the code didn't use "draw" anywhere.. What is happening? So confusing.. – ola.rogula Apr 08 '16 at 20:43
  • Weird. It's as if the phone was always testing an older version than I was coding.. I'm transferring the file between a PC and a Mac for deployment so something must be being saved wrong. But at least good news, I think this means the cameraroll code does indeed work correctly! =D – ola.rogula Apr 08 '16 at 20:49
  • Now I'm definitely using the cameraroll code but it's not working again. I have no idea what's going on. I don't even know if I'm testing correctly. I'm ready to cry. And now I have anxiety because I know someone's gonna start yelling at me that stackoverflow threads are not supposed to be this long aaaaaaah :( – ola.rogula Apr 08 '16 at 21:08
  • ...could it be that the image is too big or something? But why did it work that one time? – ola.rogula Apr 08 '16 at 21:40
  • Ooooh, okay.. so it only asks for permission once? I guess it is working! Phew~ – ola.rogula Apr 08 '16 at 21:43
  • @ola.rogula glad it work. Setup FDB so you can debug on real device. Why do you transfer it on mac. Use AIR SDK on windows, compile with command and install on device and debug. All trace statement will appear in FDB. Make sure itunes install on windows. – Sameer Kumar Jain Apr 09 '16 at 06:22
  • And yes once the permission registered it won't ask again, you can revoke permission either by deleting app first or from settings. Good luck – Sameer Kumar Jain Apr 09 '16 at 06:24
  • @SameerJain thank you very much, I didn't know I can test on a PC! Since the code works, and the issue was more to do with my n00bness at testing, if you want, you could write an answer spelling this out for me haha.. Like, very simple instructions on how to test from a PC and the easiest way to get error messages, etc. And I will choose it as the answer :) Something like, "The cameraroll code is correct. Make sure you are testing it like this:" – ola.rogula Apr 10 '16 at 17:18
  • @ola.rogula this have everything you need http://help.adobe.com/en_US/air/build/WS901d38e593cd1bac7b2281cc12cd6bced97-8000.html. Download latest AIR SDK and then use commands. Make sure you run command in proper directory. There are plenty links on google search "adobe air fdb" – Sameer Kumar Jain Apr 11 '16 at 06:12

2 Answers2

2

So... the camera roll code does actually work, just as I had it. And it is indeed a very nice and neat way to do it. I guess I was having issues with testing properly and just couldn't tell that it was working.

When working properly, it asks for permission once on the device, and after that it saves without notification, so I added some animations to hide the Saving button for a few second so the user doesn't over spam themselves, and to give a visual cue that the button was pressed.

ola.rogula
  • 307
  • 1
  • 9
1

Mobile apps behave much like Desktop apps but your code is not really adapted for that anyway. You are not even trying to use the classes specifically made for desktop and mobile like the File class for example. Anyway before I show you the steps you need to forget about doing swf for the web (which is what your code shows), you want mobile/desktop app then adapt your mind to it and use AIR classes.

First, get the data by filing a bytearray:

var byteArray:ByteArray = new ByteArray();
bitmapData.encode(new Rectangle(0,0,bitmapData.width,bitmapData.height), new PNGEncoderOptions(), byteArray);

Second, pick a destination (on mobile it's usually File.applicationStorageDirectory) and yes use the AIR class File.

var dest:File = File.applicationStorageDirectory.resolvePath("mypng.png");

Third, you got the data, you got the destination, now save using the AIR class FileStream:

var stream:FileStream = new FileStream();
stream.open(dest, FileMode.WRITE);
stream.writeBytes(byteArray);
stream.close();

And that's about it. Use the File class to create directories and files in the storage directory. Careful, on mobile you will not be allowed to save your file just anywhere and that is why using applicationStorageDirectory is better since it's always available and sandboxed for your use.

BotMaster
  • 2,233
  • 1
  • 13
  • 16
  • I found it slow if you write bitmap data in application storage directory on iOS . I use ANE which writes too fast. – Sameer Kumar Jain Apr 09 '16 at 06:26
  • The only slow operation with Adobe AIR on Ios is drawing to bitmapdata which can be n * 100ms. – BotMaster Apr 09 '16 at 13:12
  • don't think so, I tried saving a full screen captured from iphone5 and it takes a lot to save compared to ANE save the bitmapdata. – Sameer Kumar Jain Apr 09 '16 at 13:47
  • You disagree and then go onto confirming what I said, did you read my comment at all? – BotMaster Apr 09 '16 at 15:27
  • I am saying that the operation of writing through Filestream on ios is slow if you write large bitmapdata like OP have 1242,1864. – Sameer Kumar Jain Apr 09 '16 at 15:31
  • Yeah, the image is pretty big, but the cameraroll seems to work pretty fast.. I can try this too and compare once I figure out how to test better XD – ola.rogula Apr 10 '16 at 17:18
  • @ola.rogula camera roll could be fast I never test that because I want to have more control and reload if required within my app by simply showing what was saved through my app – Sameer Kumar Jain Apr 10 '16 at 17:22