Created
June 20, 2012 12:00
-
-
Save itoz/2959547 to your computer and use it in GitHub Desktop.
[AIR] ローカルに画像保存 ref: http://qiita.com/items/7ba6dcd5fe6ecbe6e196
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* copyright (c) 2012 www.itoz.jp | |
* @author itoz | |
* @since 2012/06/20 19:55:22 | |
*/ | |
package jp.itoz.air.util | |
{ | |
import flash.events.ErrorEvent; | |
import flash.filesystem.File; | |
import flash.filesystem.FileMode; | |
import flash.filesystem.FileStream; | |
import flash.utils.ByteArray; | |
import flash.events.EventDispatcher; | |
import flash.events.IEventDispatcher; | |
/** | |
* [AIR] ローカルに画像保存 | |
*/ | |
public class LocalImageSaver extends EventDispatcher | |
{ | |
public function LocalImageSaver(target : IEventDispatcher = null) | |
{ | |
super(target); | |
} | |
/** | |
* 保存 | |
* @param writeFile | |
* @param imageBytes | |
*/ | |
public function save(writeFile : File, imageBytes : ByteArray) : void | |
{ | |
var stream : FileStream = new FileStream(); | |
try { | |
stream.open(writeFile, FileMode.WRITE); | |
stream.writeBytes(imageBytes, 0, imageBytes.length); | |
} | |
catch (e : Error) { | |
dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, writeFile + " 保存できませんでした")); | |
} | |
finally { | |
stream.close(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//sample | |
_saveDir = File.userDirectory; | |
var pngBytes : ByteArray = PNGEncoder.encode(myBitmapData); | |
_saver = new LocalImageSaver(); | |
_saver.addEventListener(ErrorEvent.ERROR, onSaveError); | |
_saver.save(_saveDir.resolvePath("hoge"+"/"+_name), pngBytes); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment