iOS开发使用Starling框架:截取屏幕截图并将其保存到相机胶卷

时间:2012-05-09 17:30:54

标签: ios flash air starling-framework

我使用flash进行ios开发,我使用starling框架。 我正在尝试截取屏幕截图,然后将其保存到相机胶卷,但cameraRoll只接受bitmapdata。我如何将精灵转换为bitmapdata?万分感谢!!

3 个答案:

答案 0 :(得分:0)

您可以使用BitmapData.draw()方法将Sprite转换为BitmapDataHere就是一个例子。

答案 1 :(得分:0)

Try to get an byte array for this use can use Encoder like JPGEncoder or PNGEncoder from byte array you can easily convert to bitmapData.
And the code which i used for converting byte array to bitmapData is here. I hope it would be helpfull for you.If you send any byteArray by calling this class it would convert you bitmapData and return back using callBack Function.

package browsingImages
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.utils.ByteArray;

    import scenes.Subscenes.KeepDiary;

    public class BitmapDataConverter
    {

//      private var  _KeepDiary:KeepDiary;
        private var bmpData:BitmapData;

        private var _loader:Loader;
        private var _callB:Function;

        public function BitmapDataConverter(byteArr:ByteArray,callB:Function)
        {
            _callB=callB;
            getBitmapFunc(byteArr);
        }

        private function getBitmapFunc(bytArr:ByteArray):void{

            if(bytArr != null){
                bmpData = new BitmapData(100, 100, true,0xFFFFFF);
                _loader = new Loader();
                _loader.loadBytes(bytArr);
                _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderAdded);
            }
        }

        private function onLoaderAdded(eve:Event):void{
            _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaderAdded);
            bmpData.draw(_loader);
            bmpData = Bitmap(_loader.content).bitmapData;

            if(_callB != null)
                _callB(bmpData);
        }

    }
}

答案 2 :(得分:0)

实际上,你不能使用draw为starling的Sprite。 这段代码对我有用:

public static function copyAsBitmapData(displayObject:DisplayObject, transparentBackground:Boolean = true, backgroundColor:uint = 0xcccccc):BitmapData
{
    if (displayObject == null || isNaN(displayObject.width)|| isNaN(displayObject.height))
        return null;
    var resultRect:Rectangle = new Rectangle();
    displayObject.getBounds(displayObject, resultRect);

    var result:BitmapData = new BitmapData(displayObject.width, displayObject.height, transparentBackground, backgroundColor);
    var context:Context3D = Starling.context;
    var support:RenderSupport = new RenderSupport();
    RenderSupport.clear();
    support.setOrthographicProjection(0, 0, Starling.current.stage.stageWidth, Starling.current.stage.stageHeight);
    support.applyBlendMode(true);
    support.translateMatrix( -resultRect.x, -resultRect.y);
    support.pushMatrix();
    support.blendMode = displayObject.blendMode;
    displayObject.render(support, 1.0);
    support.popMatrix();
    support.finishQuadBatch();
    context.drawToBitmapData(result);
    return result;
}