1

我用两个 jpeg 在 flash 中生成一个透明位图。一张是黑白的,用于读取 BitmapDataChannel,另一张图片是实际图像。

这是我的代码:

private var sourceBitmap:BitmapData;
private var alphaBitmap:BitmapData;
private var outputBitmap:BitmapData;
private var bitmap:Bitmap;

//Updated Code:
private var sourceByteArray:ByteArray = new ByteArray(), outputByteArray:ByteArray = new ByteArray(), alphaByteArray:ByteArray = new ByteArray();
private var jpegencoderoptions = new JPEGEncoderOptions(0);

//get image from library
this.sourceBitmap = new imageFromLibrary();
//Updated: encode sourceBitmap
this.sourceBitmap.encode(this.sourceBitmap.rect, jpegencoderoptions, this.sourceByteArray);
//create new bitmapData with dimensions of sourcimage.      
this.outputBitmap = new BitmapData(this.sourceBitmap.width, this.sourceBitmap.height, true, 0x00000000);
//Updated: encode outputBitmap
this.outputBitmap.encode(this.outputBitmap.rect, jpegencoderoptions, this.outputByteArray);

this.outputBitmap.lock();

//copy the pixels from source to output
this.outputBitmap.copyPixels(this.sourceBitmap, this.sourceBitmap.rect, new Point());
//free memory and dispose source
this.sourceBitmap.dispose();
//Updated: clear sourceByteArray
this.sourceByteArray.clear();

//get alphamap from library         
this.alphaBitmap = new alphamapFromLibrary();
//Updated: encode alphaBitmap 
this.alphaBitmap.encode(this.alphaBitmap.rect, jpegencoderoptions, this.alphaByteArray);

//copy the channel from alpha to output
this.outputBitmap.copyChannel(this.alphaBitmap, this.alphaBitmap.rect, new Point(), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);

//free memory and dispose alpha
this.alphaBitmap.dispose();
//Updated: clear alphaByteArray
this.alphaByteArray.clear();

this.bitmap = new Bitmap(this.outputBitmap);

该代码效果很好,它为我生成了一个透明图像。但我现在的问题是沉重的 RAM 负载。在较旧的智能手机上,它超过了 RAM,游戏正在自行关闭。

主要问题是:我加载了 sourceBitmap,这需要 RAM,然后我生成新的 BitmapData“outputBitmap”。有了这个,我在 RAM 中有 2 倍的 BitmapData 具有相同的尺寸。

我现在的问题是:我可以跳过一些步骤并加载我的 sourceBitmap、将透明设置为 true 并将通道复制到此源而不是加载源、创建输出等等吗?

编辑:

我现在更新了我的代码,BitmapData.encode()将我的 BitmapDatas 编码为 JPEG。但内存使用情况并不总是发生变化。有时编码后内存使用量增加,有时减少,有时没有变化。

4

0 回答 0