Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075919 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:29:13 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Saving an image in Flash
Pages: [1] 2
Print
Author Topic: Saving an image in Flash  (Read 1638 times)
Sean A.
Level 8
***



View Profile Email
« on: August 29, 2011, 07:21:14 PM »

So I'm working on a small application in Flash/Flashpunk and I need to have to ability to save images. I know that it is possible using the FileReference class but I need to be able to do it without a popup window because the saving is supposed to be automated. Looking around I also stumbled upon the idea of saving a byte array as a string and then saving that and then loading the text and converting it back into a ByteArray but I hear that it has unwieldy results. So if there are any other methods your help would be appreciated.
Logged
blinkok
Level 1
*



View Profile
« Reply #1 on: August 29, 2011, 08:26:05 PM »

http://www.learningactionscript3.com/downloads/

chapter 9: drawing with pixels (encode and save)

hope this helps

some more;

https://github.com/mikechambers/as3corelib
« Last Edit: August 29, 2011, 08:41:38 PM by blinkok » Logged

grapefrukt
Level 1
*



View Profile WWW Email
« Reply #2 on: August 29, 2011, 10:18:07 PM »

You won't be able to save/read local files without a popup window in "regular" flash, it'd be a pretty huge security issue if that was possible. You're stuck with using FileReference atleast to save.

What I'd do in your situation is either write a small php-script and call out to that (means you need to be running a local webserver) or use Air, that will give you actual access to the file system.
Logged
Loren Schmidt
Level 10
*****



View Profile WWW Email
« Reply #3 on: August 29, 2011, 10:27:12 PM »

One thing you could do is create some sort of array from your image data, and then use SharedObject to save it. This would let you get around the need for a dialog box.
Logged
bateleur
Level 10
*****



View Profile
« Reply #4 on: August 30, 2011, 02:56:52 AM »

One thing you could do is create some sort of array from your image data, and then use SharedObject to save it. This would let you get around the need for a dialog box.
The trouble with this is you might fail to save the image if the user hasn't allowed enough SharedObject space for the image data.
Logged

st33d
Guest
« Reply #5 on: August 30, 2011, 12:43:17 PM »

ByteArray comes with a compression option. You can simply load the image into a ByteArray, compress and then save to the SharedObject. Being an image though, it will only compress so much.

As mentioned before, saving to disk is impossible without a Dialog. Though in a browser you could transmit the file to a server. And lastly, one of my co-workers wrote a C# wrapper for a swf so that we could run level editors with auto-save features. So there's always that last posibility.
Logged
increpare
Guest
« Reply #6 on: August 30, 2011, 12:57:18 PM »

As mentioned before, saving to disk is impossible without a Dialog. Though in a browser you could transmit the file to a server. And lastly, one of my co-workers wrote a C# wrapper for a swf so that we could run level editors with auto-save features. So there's always that last posibility.
Why not use air in such a circumstance?
Logged
SFBTom
Level 1
*



View Profile WWW
« Reply #7 on: August 30, 2011, 02:34:59 PM »

When using the SharedObject method, the Flash Player defaults to 100kB of storage space. If you never try to save more than this (in total at any one time), then the user won't get a dialogue, unless they've previously turned down the storage limit.

If you do go over 100kB (or the user's set limit), then yes, they'll get a dialogue asking them to turn it up. If they do, that persists, so they shouldn't see the dialogue ever again.

If it's images you're storing, then your best bet is compressing them as either jpgs or pngs, depending on your needs. as3corelib has utilities for both compression types. You should then be able to save quite a few images before hitting the 100kB limit.
Logged

Sean A.
Level 8
***



View Profile Email
« Reply #8 on: August 30, 2011, 04:31:19 PM »

Thanks for all the replies, for the most part this application will be running locally using a projector rather than being hosted and that probably opens up more possibilities so is there a way I can use that to my advantage? Also the images are fairly large so the 100kb limit is a tad small. I am already using the JPEGEncoder in as3corelib but the images are quite large and there will be many of them, as in like 100+ images.
Logged
SFBTom
Level 1
*



View Profile WWW
« Reply #9 on: August 30, 2011, 04:41:51 PM »

A method I've had good success with is calling the mySharedObject.flush(512000) at some point during your startup menus, where the number is the max filesize you'll need in bytes. You can accompany it with an explanation of what you're going to use the storage for, but skip that screen if it returns SharedObjectFlushStatus.FLUSHED - the user already has their limit set high enough. Again, assuming they agree to the increase in limit, they'll never see that dialogue again, even when they next load up your game. When it comes time to save your data, you can do so safe in the knowledge that nothing will get triggered.
Logged

st33d
Guest
« Reply #10 on: August 31, 2011, 02:47:39 AM »

As mentioned before, saving to disk is impossible without a Dialog. Though in a browser you could transmit the file to a server. And lastly, one of my co-workers wrote a C# wrapper for a swf so that we could run level editors with auto-save features. So there's always that last posibility.
Why not use air in such a circumstance?

The big issue with our previous level editors was all the fannying about to get it to save directly (setting up permissions in the browser, etc).

I think installing AIR and then reinstalling an AIR application every time you add a few more tiles is a bit much. Especially for our artists.

The C# wrapper just grabs the editor.swf and then runs it fullscreen with ExternalInterface calling the save option on the wrapper. No muss, no fuss. The artist doesn't even need to know how to install stuff. It just works from an exe file.
Logged
Sean A.
Level 8
***



View Profile Email
« Reply #11 on: September 03, 2011, 03:10:29 PM »

Alright again thanks for all the suggestions, I took a quick look at the sharedObject thing but I'm not really sure how that works. I looked at Adobe's documentation and examples for it, but I couldn't really figure out how to actually save the data. So if someone who has used it before could just kinda walk me through turning my compressed image as a ByteArray into a saved image on the hard drive that would be incredible.
Logged
st33d
Guest
« Reply #12 on: September 03, 2011, 05:11:17 PM »

I've used this method here:

https://github.com/st33d/red-rogue/blob/master/src/com/robotacid/ui/QuickSave.as

You should be able to just pack the BitmapDatas into an Object. Though I haven't tried it.

For what you're doing it doesn't sound very viable if you've got hundreds of images. Flash simply isn't built for storing a ton of information to disk.
Logged
Sean A.
Level 8
***



View Profile Email
« Reply #13 on: September 03, 2011, 09:47:26 PM »

Well is there a way to copy/move files around? Because all of the images I'm saving have already been loaded from the users hard drive. So instead of saving if I could just copy from one location to a new location that would work too.
Logged
st33d
Guest
« Reply #14 on: September 04, 2011, 02:42:40 AM »

Unless you are using AIR, categorically no.

You cannot modify files or gather directory information from Flash. This is a security restriction. If you could, then it would effectively be a platform for writing trojans.

If you must work in Flash and you must have access to the harddrive then I would suggest looking into exporting your Flash project to AIR as a compromise.

Sorry mate.
Logged
Sean A.
Level 8
***



View Profile Email
« Reply #15 on: September 04, 2011, 07:10:42 AM »

Well that's a real bitch. I guess I'll try and work something out without saving or copying. Is there no way to save an image as a string in a text file without losing any information? Because that's the only way I could see this working. The images I save from this application will be loaded into another application so if I can turn that string I've created back into an image once it's been loaded then that solution would work.
Logged
st33d
Guest
« Reply #16 on: September 04, 2011, 07:24:39 AM »

You cannot save ANY type of file - unless you use the FileReference class. The FileReference class will open a file browser and even that must be initiated by a mouse click.
Logged
Sean A.
Level 8
***



View Profile Email
« Reply #17 on: September 04, 2011, 07:49:55 AM »

Well that's fine if I had the ability to put images into strings because I could convert all of my images into strings and then pack them all into a single text file which would be saved using the FileReference class and then loaded into the second application later. I'm just trying to avoid the user having to save each image manually.
Logged
st33d
Guest
« Reply #18 on: September 04, 2011, 12:16:20 PM »

I see, FileReference is what you need then.

I'm not sure if this will work, but you could try doing:

Code:
var obj:Object = {};
for(var i:int = 0; i < imageList.length; i++){
  obj[i] = imageList[i];
}
var byteArray = new ByteArray();
byteArray.writeObject(obj);
byteArray.compress();

I'm not entirely sure though if the BitmapDatas will stay intact through the whole process. You could give it a go I guess. Otherwise there's your String conversion that you could attach to the object and those are bound to survive.
Logged
Sean A.
Level 8
***



View Profile Email
« Reply #19 on: September 04, 2011, 05:25:12 PM »

So the code you posted there is that assuming my image list array contains ByteArrays? Also after I write the final byteArray to a text file how would I split it back out into all of the different images on the other end?
Logged
Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic