Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 生成带有alphamap RAM使用问题的透明JPEG_Actionscript 3_Flash_Bitmap_Bitmapdata - Fatal编程技术网

Actionscript 3 生成带有alphamap RAM使用问题的透明JPEG

Actionscript 3 生成带有alphamap RAM使用问题的透明JPEG,actionscript-3,flash,bitmap,bitmapdata,Actionscript 3,Flash,Bitmap,Bitmapdata,我用两个JPEG在flash中生成一个透明位图。一个是黑白的,用于读取BitmapDataChannel,另一个图片是实际图像 这是我的代码: private var sourceBitmap:BitmapData; private var alphaBitmap:BitmapData; private var outputBitmap:BitmapData; private var bitmap:Bitmap; //Updated Code: private var sourceByteAr

我用两个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,游戏正在自行关闭

主要问题在于: 我加载sourceBitmap,这需要RAM,然后生成新的BitmapData outputBitmap。有了这个,我在RAM中有了两个相同尺寸的位图数据

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

编辑:


我现在用BitmapData.encode更新了代码,将BitmapData编码为JPEG。但内存使用并不总是有变化。有时编码后内存使用增加,有时减少,有时不变。

图像的尺寸是多少?我认为PNG编码是不可能的?不是100%确定你想创建什么,但我想我有一个想法。如果像素通过alpha测试,则可以尝试通过循环像素并直接将其写入新位图。除非这些图片很大,否则我不认为3张图片会给你带来记忆问题。你的记忆很有可能被其他地方占用,而这些功能只是把它推到了极限。您还可以尝试降低图像的PPI。我的背景图像大小约为5000 x 1600,内存约为32MB,内存约为64MB。我用getPixel和setPixel测试了它,可以在所有像素之间循环。但这需要太长时间@Michael Brewer Davis我能用PNG编码做什么?如果imageFromLibrary是PNG,它可以包括alpha通道,并且你不需要合并两个图像。我也觉得奇怪的是,你用PNG而不是JPEG有性能问题。。。我会再检查一遍。另外,一种可能的解决方案是将原始位图拆分为几个较小的位图,然后以较小的步骤合成较大的位图,这样,内存中就不会同时有两个5000x1600位图,而只有一个5000x1600,假设一个1000x800,这样就可以得到10个步骤。额外的一英里将是永远不会真正使用这样一个大的位图,但只有在内存中你需要在游戏的不同阶段根据你的游戏需要。