Delphi FMX correctly loads images to minimize memory usage (two TImageList)

Delphi FMX correctly loads images to minimize memory usage (two TImageList)

        After the last article "Delphi FMX correctly loads images to minimize memory usage (one of TBitmapSurface)", we continue to optimize memory. This video talks about FMX TImageList loading SD large images.

        In the past, when we used TImageList to load small icons, we generally did not pay attention to memory consumption issues. In the past, we did not care about these issues during VCL programming, just rush forward. However, when using TImageList under FMX to load a large number of SD left and right pixel size dynamic pictures as a menu (for example: 593px*593px), you will find that if you extract the bitmap of the image resource according to the normal usage, the memory resource consumption is the price very big.

        1. The wrong approach

        1.1. Wrong approach 1: (Ibid.)

//错误做法1:TImage.Bitmap.LoadFromStream 或 TImage.Bitmap.LoadFromFile
          //内存流太吃内存了,特别又是循环中:
          // 创建流,用流来转换图片,实际应用时可直接将图片处理成流来应用
index :=0;  num :=8; 
//...................
        s.cx := 96;  s.cy := 96;//:屏幕的点阵,非像素:设置为Image的宽高
		for i := index to num + index - 1 do
		begin
//...............................
          TS := TMemoryStream.Create;
          //加载TImageList中的图标
          ilMainImage.Bitmap(s, i).SaveToStream(TS); // 生成流数据
          TS.Position := 0;
          Image.Bitmap.LoadFromStream(TS); //:流数据载入  Image
          TS.DisposeOf;
//...............................
		end;

        1.2. Wrong approach 2:

index :=0;  num :=8; 
//...................
        s.cx := 96;  s.cy := 96;//:屏幕的点阵,非像素:设置为Image的宽高
		for i := index to num + index - 1 do
		begin
//...............................
          Image.Bitmap.Assign( ilMainImage.Bitmap( s,i ) );//:94个图片能节省23M内存后:但也大
//...............................
		end;

        1.3. Wrong approach 3:

index :=0;  num :=8; 
//...................
        s.cx := 96;  s.cy := 96;//:屏幕的点阵,非像素:设置为Image的宽高
		for i := index to num + index - 1 do
		begin
//...............................
            LBitmapSurface :=TBitmapSurface.Create;//:创建客制化位图界面
            LBitmapCodec :=TBitmapCodecManager.Create;
            try
              LBitmapSurface.SetSize(96,96);
                LBitmapCodec.LoadFromFile(
                  ilMainImage.Source[i].MultiResBitmap[0].FileName,//:FileName已含路径
                  LBitmapSurface, 96 );
              Image.Bitmap.Assign( LBitmapSurface );
            finally
              LBitmapSurface.DisPoseOf;  LBitmapCodec.DisPoseOf;
            end;
//...............................
		end;

        2. Correctly use TImageList to load the Bitmap of the image to Image

index :=0;  num :=8; 
//...................
        s.cx := 96;  s.cy := 96;//:屏幕的点阵,非像素:设置为Image的宽高
		for i := index to num + index - 1 do
		begin
//...............................
          //ilMainImage.ClearCache(i);//:占用2M内存
          //(ilMainImage.Destination[i].Layers[0].MultiResBitmap[0].Bitmap ).Width:=96;//:无需
          //(ilMainImage.Destination[i].Layers[0].MultiResBitmap[0].Bitmap ).Height:=96;//:无需
          (ilMainImage.Destination[i].Layers[0].MultiResBitmap[0].Bitmap ).Assign( ilMainImage.Bitmap( s,i ) );
          Image.Bitmap.Assign( ilMainImage.Destination[i].Layers[0].MultiResBitmap[0].Bitmap );
          //ilMainImage.Bitmap( s,i ).FreeHandle; //:占用5M内存
//...............................
		end;

Related blog posts on this blog:

      1. "Delphi FMX loads pictures correctly to minimize memory usage (one of TBitmapSurface)"

      https://blog.csdn.net/pulledup/article/details/108935897

      2. "Delphi code and ideas for handling high-speed file upload and download"

      https://blog.csdn.net/pulledup/article/details/108660481

If you like it, just click like and favorite below, so that you can watch the next sharing:

 

Guess you like

Origin blog.csdn.net/pulledup/article/details/108979086