[英]Take a screenshot and then save it to the camera roll. Swift 4.2, Xcode 10
[英]iOS deve with Starling framework: take a screenshot and save it to the camera roll
我将Flash用于ios开发,并且使用starling框架。 我正在尝试截取屏幕截图,然后将其保存到相机胶卷中,但是cameraRoll仅接受位图数据。 我该如何将精灵转换为位图数据? 太感谢了!!
您可以使用BitmapData.draw()方法将Sprite
转换为BitmapData
。 这是一个例子。
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);
}
}
}
实际上,您不能对starling的Sprite使用draw。 该代码对我有用:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.