Delphi XE4 FireMonkey / iOS DrawBitmap不能正常工作

时间:2013-07-17 09:03:18

标签: ios delphi firemonkey

因此,通过我的例行程序,我计算出了想要在TImage中显示的TBitmap( ABitmap )的新宽度/高度。

以下是调整大小并绘制 ABitmap

的代码
var
  TmpScale: Double;
  TmpNH: Single;
  TmpNW: Single;
  P: string;
begin      
  P := FAppDataDirPath + 'Library' + PathDelim + 'assets' + PathDelim + 'app_header.png';
  if FileExists(P) then
    begin
      ImageLogo.Bitmap := TBitmap.Create(1,1);
      ImageLogo.Bitmap.LoadFromFile(P);

      TmpScale := ImageLogo.Width / ImageLogo.Bitmap.Width;
      TmpNW := ImageLogo.Bitmap.Width * TmpScale;
      TmpNH := ImageLogo.Bitmap.Height * TmpScale;

      FBitmapBuffer.Width := Round(TmpNW);
      FBitmapBuffer.Height := Round(TmpNH);
      FBitmapBuffer.Canvas.DrawBitmap(ImageLogo.Bitmap, RectF(0,0,ImageLogo.Bitmap.Width,ImageLogo.Bitmap.Height), RectF(0,0,TmpNW,TmpNH), 255);
      ImageLogo.Bitmap.Assign(FBitmapBuffer);

我得到的只是一个显示TImage的完全空白区域。

1 个答案:

答案 0 :(得分:6)

看一下TCanvas.BeginScene(..):在画布上绘画之前,你必须开始场景,然后结束

if yourCanvas.BeginScene() then
    try
        yourCanvas.DrawBitmap(..);
    finally
        yourCanvas.EndScene();
    end;

就是这样:你只是错过了对BeginScene()EndScene()的来电。但除此之外,我有几点建议:

建议

  1. 您不必创建具有伪造分辨率的位图,只需从磁盘加载内容即可。只需使用交替构造函数TBitmap.CreateFromFile(const AFileName: string)

  2. 您还需要确保FBitmapBuffer指向有效的TBitmap对象。您应该插入if not Assigned(FBitmapBuffer) then FBitmapBuffer := TBitmap.Create(..);

  3. 之类的支票
  4. 此外,您每次都在创建一个新的TBitmap对象,并且在完成工作后您不会将其释放。每当你打电话给你的程序时,你的记忆就会被TBitmaps填满你再也看不到了。你应该在程序结束时释放它。

  5. 另外,您确定,例如,将新位图的宽度设置为Round(TmpNW)是您想要的吗?我想你打算改为Trunc(TmpNW)

  6. 为什么使用firemonkey组件的位图从磁盘而不是FBitmapBuffer对象加载图像?

  7. 最后:尽管我们已经将位图粘贴到TImage现在正在工作,但老实说,我不知道你最初想要做什么。在保持纵横比的同时将位图粘贴到图像上? TiImage具有WrapMode属性,可让您设置图像的缩放方式。