Форум Flasher.ru

Форум Flasher.ru (http://www.flasher.ru/forum/index.php)
-   ActionScript 3.0 (http://www.flasher.ru/forum/forumdisplay.php?f=83)
-   -   Проблема с ColorMatrixFilter (http://www.flasher.ru/forum/showthread.php?t=140068)

antprobe 19.05.2010 16:21

Проблема с ColorMatrixFilter
 
Подскажите пожалуйста почему растет память
Код AS3:

package app
{
        import flash.system.Security;
        import flash.system.System;
        import flash.display.*;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.text.*;
        import flash.filters.ColorMatrixFilter;
        import flash.geom.Point;
 
        public class MemoryTest extends Sprite
        {
                protected var _leftBitmap:Bitmap;
                protected var _rightBitmap:Bitmap;
                protected var _convertedBitmap:Bitmap;
                protected var _memoryInfo:TextField;
 
                public function MemoryTest()
                {
                        _leftBitmap = getBitmap(LeftImage);
                        _rightBitmap = getBitmap(RightImage);
 
                        _memoryInfo = new TextField();
                        stage.addChild(_memoryInfo);
 
                        stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
                }
                protected function getBitmap(src:Class)
                {
                        var image = new src();
                        var bd = new BitmapData(image.width,image.height);
                                bd.draw(image);
                        var bm = new Bitmap(bd);
 
                        return bm;
                }
                protected function mouseDownHandler(evt:MouseEvent=null)
                {       
                        if (_convertedBitmap)
                        {
                                clearBitmap(_convertedBitmap);
                                _convertedBitmap.parent.removeChild(_convertedBitmap);
                        }
                        _memoryInfo.text = "Memory: "+System.totalMemory/(1024);
                        _memoryInfo.autoSize = "left";
 
                        var leftBitmapMatrix = new Array();
                                leftBitmapMatrix = leftBitmapMatrix.concat([0.299, 0.587, 0.114, 0, 0]); // red
                                leftBitmapMatrix = leftBitmapMatrix.concat([0, 0, 0, 0, 0]); // green
                                leftBitmapMatrix = leftBitmapMatrix.concat([0, 0, 0, 0, 0]); // blue
                                leftBitmapMatrix = leftBitmapMatrix.concat([0, 0, 0, 1, 0]); // alpha
 
                        var rightBitmapMatrix = new Array();
                                rightBitmapMatrix = rightBitmapMatrix.concat([0, 0, 0, 0, 0]); // red
                                rightBitmapMatrix = rightBitmapMatrix.concat([0, 1, 0, 0, 0]); // green
                                rightBitmapMatrix = rightBitmapMatrix.concat([0, 0, 1, 0, 0]); // blue
                                rightBitmapMatrix = rightBitmapMatrix.concat([0, 0, 0, 1, 0]); // alpha
 
                        _convertedBitmap = new Bitmap(new BitmapData (_leftBitmap.width, _leftBitmap.height, true, 0));
 
                        var proccessedLeftBitmap = getProccessedBitmap(_leftBitmap,new ColorMatrixFilter(leftBitmapMatrix));
                        var proccessedRightBitmap = getProccessedBitmap(_rightBitmap,new ColorMatrixFilter(rightBitmapMatrix));
 
                        _convertedBitmap.bitmapData.draw(proccessedLeftBitmap, null, null, BlendMode.ADD);
                        _convertedBitmap.bitmapData.draw(proccessedRightBitmap, null, null, BlendMode.ADD);
 
                        addChild(_convertedBitmap);
 
                        clearBitmap(proccessedLeftBitmap);
                        clearBitmap(proccessedRightBitmap);
                }
                protected function clearBitmap(bitmap:Bitmap)
                {
                        if (bitmap.bitmapData)
                        {
                                bitmap.bitmapData.dispose();
                                bitmap.bitmapData = null;
                                bitmap = null;
                        }
                }
                protected function applyFilter(bitmap:Bitmap,colorMatrixFilter:ColorMatrixFilter)
                {
                        var filters:Array = new Array();
                                filters.push(colorMatrixFilter);
 
                        bitmap.filters = filters;
                }
                protected function getProccessedBitmap(srcBitmap:Bitmap,filter:ColorMatrixFilter)
                {
                        var proccessedBitmapData = srcBitmap.bitmapData.clone();
                        var proccessedBitmap = new Bitmap(proccessedBitmapData);
                                applyFilter(proccessedBitmap,filter);
 
                        return proccessedBitmap;
                }
 
        }
 
}

Если закоментировать строчку applyFilter(proccessedBitmap,filter); в функции getProccessedBitmap(srcBitmap:Bitmap,filter:ColorMatrixFilter), память почти не увеличиваеться, а с ней стремительно растет после каждого нажатия кнопки мышки...

Psycho Tiger 19.05.2010 19:13

Код AS3:

var proccessedBitmapData = srcBitmap.bitmapData.clone();

Вы клонируете битмапу и сохраняете на неё ссылки в экземпляре Bitmap. Конечно, память будет расти.

antprobe 19.05.2010 19:20

Цитата:

Сообщение от Psycho Tiger (Сообщение 909399)
Код AS3:

var proccessedBitmapData = srcBitmap.bitmapData.clone();

Вы клонируете битмапу и сохраняете на неё ссылки в экземпляре Bitmap. Конечно, память будет расти.

Клонирую а потом вызываю метод
Код AS3:

clearBitmap(proccessedLeftBitmap);
clearBitmap(proccessedRightBitmap);

и очищаю... а память всеравно растет :(

Знакомый попробывал скомпилировать у себя на Flash CS3 у него работает немного по другому, память увеличиваеться до определенного момента, а потом очищаеться, я компилировал на Flash CS5 у меня не очищаеться... бред какой-то...

Добавлено через 18 часов 45 минут
Действительно проблема была в ссылках на Bitmap
Код AS3:

package app
{
        import flash.system.Security;
        import flash.system.System;
        import flash.display.*;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.text.*;
        import flash.filters.ColorMatrixFilter;
        import flash.geom.Point;
 
        public class MemoryTest extends Sprite
        {
                protected var _leftBitmap:Bitmap;
                protected var _rightBitmap:Bitmap;
                protected var _convertedBitmap:Bitmap;
                protected var _memoryInfo:TextField;
                protected var _leftBitmapMatrix:Array;
                protected var _rightBitmapMatrix:Array;
                protected var _testFilter:*;
 
                public function MemoryTest()
                {
                        _leftBitmap = getBitmap(LeftImage);
                        _rightBitmap = getBitmap(RightImage);
 
 
 
                        _memoryInfo = new TextField();
                        stage.addChild(_memoryInfo);
 
                        _leftBitmapMatrix = new Array();
                        _leftBitmapMatrix = _leftBitmapMatrix.concat([0.299, 0.587, 0.114, 0, 0]); // red
                        _leftBitmapMatrix = _leftBitmapMatrix.concat([0, 0, 0, 0, 0]); // green
                        _leftBitmapMatrix = _leftBitmapMatrix.concat([0, 0, 0, 0, 0]); // blue
                        _leftBitmapMatrix = _leftBitmapMatrix.concat([0, 0, 0, 1, 0]); // alpha
 
                        _rightBitmapMatrix = new Array();
                        _rightBitmapMatrix = _rightBitmapMatrix.concat([0, 0, 0, 0, 0]); // red
                        _rightBitmapMatrix = _rightBitmapMatrix.concat([0, 1, 0, 0, 0]); // green
                        _rightBitmapMatrix = _rightBitmapMatrix.concat([0, 0, 1, 0, 0]); // blue
                        _rightBitmapMatrix = _rightBitmapMatrix.concat([0, 0, 0, 1, 0]); // alpha
 
                        stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
 
                        applyFilter(_leftBitmap,new ColorMatrixFilter(_leftBitmapMatrix));
                        applyFilter(_rightBitmap,new ColorMatrixFilter(_rightBitmapMatrix));
                }
                protected function getBitmap(src:Class)
                {
                        var image = new src();
                        var bd = new BitmapData(image.width,image.height);
                                bd.draw(image);
                        var bm = new Bitmap(bd);
 
                        return bm;
                }
                protected function mouseDownHandler(evt:MouseEvent=null)
                {       
                        if (_convertedBitmap)
                        {
                                clearBitmap(_convertedBitmap);
                                _convertedBitmap.parent.removeChild(_convertedBitmap);
                        }
                        _memoryInfo.text = "Memory: "+System.totalMemory/(1024);
                        _memoryInfo.autoSize = "left";
 
                        _convertedBitmap = new Bitmap(new BitmapData (_leftBitmap.width, _leftBitmap.height, true, 0));
 
                        _convertedBitmap.bitmapData.draw(_leftBitmap, null, null, BlendMode.ADD);
                        _convertedBitmap.bitmapData.draw(_rightBitmap, null, null, BlendMode.ADD);
 
                        addChild(_convertedBitmap);
                }
                protected function clearBitmap(bitmap:Bitmap)
                {
                        if (bitmap.bitmapData)
                        {
                                bitmap.bitmapData.dispose();
                                bitmap.filters = [];
                                bitmap.bitmapData = null;
                                bitmap = null;
 
                        }
                }
                protected function applyFilter(bitmap:Bitmap,colorMatrixFilter:*)
                {
                        var filters:Array = new Array();
                                filters.push(colorMatrixFilter);
 
                        bitmap.filters = filters;
                }
                protected function getProccessedBitmap(srcBitmap:Bitmap,filter:*)
                {
                        var proccessedBitmapData = srcBitmap.bitmapData.clone();
                        var proccessedBitmap = new Bitmap(proccessedBitmapData);
                                applyFilter(proccessedBitmap,filter);
 
                        return proccessedBitmap;
                }
 
        }
 
}

Так все работает стабильно

Спасибо


Часовой пояс GMT +4, время: 13:19.

Copyright © 1999-2008 Flasher.ru. All rights reserved.
Работает на vBulletin®. Copyright ©2000 - 2026, Jelsoft Enterprises Ltd. Перевод: zCarot
Администрация сайта не несёт ответственности за любую предоставленную посетителями информацию. Подробнее см. Правила.