[]帮忙看看Texture类getPixels内存泄漏的问题。
请帮忙看看这个demo,Texture类方法getPixels导致内存泄漏。
用的引擎版本是1.7.16beta。
用的引擎版本是1.7.16beta。
package
{
import laya.display.Sprite;
import laya.events.Event;
import laya.maths.Point;
import laya.resource.HTMLCanvas;
import laya.resource.Texture;
import laya.utils.Handler;
import laya.utils.Stat;
import laya.webgl.WebGL;
public class Main
{
private var _tex:Texture;
private var _htmlCanvas:HTMLCanvas;
private var _colorSpr:Sprite;
private var _colorTex:Texture;
private var _initPixelCount:int;
public function Main()
{
Laya.init(1024, 768, WebGL);
Stat.show(0, 0);
Laya.stage.bgColor = "#fff";
Laya.loader.load("assets/images/color1.png", Handler.create(this, __loadImageHandler));
}
private function __loadImageHandler():void
{
_colorSpr = new Sprite();
_colorTex = Laya.loader.getRes("assets/images/color1.png");
_colorSpr.graphics.drawTexture(_colorTex);
_colorSpr.cacheAs = "bitmap";
_colorSpr.pos((1024 - _colorTex.width) >> 1, (768 - _colorTex.height) >> 1);
_colorSpr.size(_colorTex.width, _colorTex.height);
Laya.stage.addChild(_colorSpr);
_initPixelCount = getPixel(_colorTex);
addEvent();
}
private function addEvent():void
{
_colorSpr.on(Event.MOUSE_MOVE, this, __moveHandler);
}
private function __moveHandler():void
{
var p:Point = toLocal(new Point(Laya.stage.mouseX, Laya.stage.mouseY), _colorSpr);
var cir:Sprite = new Sprite();
cir.graphics.drawCircle(0, 0, 15, "#fff");
cir.pos(p.x, p.y);
cir.blendMode = "destination-out";
_colorSpr.addChild(cir);
_htmlCanvas = _colorSpr.drawToCanvas(_colorTex.width, _colorTex.height, _colorSpr.x, _colorSpr.y);
_tex = new Texture(_htmlCanvas);
/************调用该方法导致内存泄漏*************/
var pixelCount:int = getPixel(_tex);
/************调用该方法导致内存泄漏*************/
if(pixelCount / _initPixelCount < 0.3)
{
trace("remove");
_htmlCanvas.clear();
_htmlCanvas.destroy();
_tex.destroy();
_colorSpr.off(Event.MOUSE_MOVE, this, __moveHandler);
}
}
private function getPixel(tex:Texture):int
{
var pixelCount:int = 0;
/************getPixels内存泄漏*************/
var arr:Array = tex.getPixels(0, 0, tex.width, tex.height);
/************getPixels内存泄漏*************/
for(var i:int = 0; i < arr.length; i++)
{
if(arr > 0)
{
pixelCount++;
}
}
return pixelCount;
}
private function toLocal(point:Object,mc:Sprite):Point
{
/*
* 参数point可以是Point类型和DisplayObject类型
* mc是目标对象
*/
var p:Object;
if(point is Sprite) {
p = new Point();
p.x = point.x;
p.y = point.y;
p = localToGlobal(Sprite(point));
}else if(point is Point) {
p = point;
}
var pp:Point = mc.globalToLocal(Point(p));
return pp;
}
private function localToGlobal(mc:Sprite):Point
{
var p:Point = new Point();
p.x = mc.x;
p.y = mc.y;
var pp:Point = (mc.parent as Sprite).localToGlobal(p);
return pp;
}
}
}
没有找到相关结果
已邀请:
要回复问题请先登录
3 个回复
Charles
赞同来自:
Charles
赞同来自:
Charles
赞同来自: