简体   繁体   中英

iOS deve with Starling framework: take a screenshot and save it to the camera roll

I use flash for ios development and I use starling framework. I'm trying to take a screenshot and then save it to the camera roll, but cameraRoll only accepts bitmapdata. How am I going to convert sprite to bitmapdata? Thanks a million!!

You can convert Sprite to BitmapData using BitmapData.draw() method. Here 's an example.

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);
        }

    }
}

Actually, you cant use draw for starling's Sprite. this code works for me:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM