FireMonkey iOS RAD Studio XE2 - Display Image on form loaded from URL

扶醉桌前 提交于 2019-12-03 22:44:06

Drop a TButton, TImageControl and TIdHttp onto a Firemonkey form and this code will pull down an image from the web:

procedure TForm1.btnReadWebImgClick(Sender: TObject);
begin
  ReadWebImage('http://www.gravatar.com/avatar/5af5f8c5f88c6c237745e9472a31410f?s=32&d=identicon&r=PG');
end;
procedure TForm1.ReadWebImage(imgAddress: string);
var
  memStream: TMemoryStream;
begin
  memStream := TMemoryStream.Create;
  try
    idhttp1.Get (imgAddress,memStream);
  except
    ShowMessage('Image not found at:'+imgAddress);
    memStream.Free;
    exit;
  end;
  try
    memStream.Position := 0;
    ImageControl1.Bitmap.LoadFromStream(memStream);
  finally
    memStream.Free;
  end;
end;

The answer works with a little freezing. I load 4 images in a loop. When I push the button, the program freeze to download the images, the progress bar not working and if are downloaded continue perfectly. For 4 images freeze for 1 sec. For 50 it's a "no connection" or "Bad Program". This is the code.

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  i: Integer;
  Stream: TMemoryStream;
  imgAddress: string;
begin
  ProgressBar1.Min := 0;
  ProgressBar1.Max := Table1.RecordCount;

   for i := 1 to Table1.RecordCount do
     begin
     ProgressBar1.Value := i;

     imgAddress := VirtualTable1.FieldByName('flyer').AsString;
     Stream := TMemoryStream.Create;
     idhttp1.Get (imgAddress,Stream);

     try
        Stream.Position := 0;
        Table1.Edit;
        TBlobField(Table1.FieldByName('image')).LoadFromStream(Stream);
        Table1.Post;
     finally
        Stream.Free;
     end;
     Table1.Next;
    end;
    Table1.First;
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!