Url Image Locking Delphi Mobile Application

Asked

Viewed 150 times

0

I am inciante in Mobile and I am in the following situation: have 3 listview that receives data via Rest from a PHP site

using restClient, Restrequest and restResponse... data gets certificate, popula alistvew all correctly, compiles also everything normal.... the problem comes next.....

in Rest has an image field (jpg,png) only the image url, use this function to convert the image to Bitmap

function TfrmPrincipal.LerImage(url: string): TBitmap;
 var
 strm : TmemoryStream;
 begin
    strm := TMemoryStream.Create;
    try
          http.Get(url,strm);
          strm.Position := 0;
          Result  := TBitmap.Create;
          result.Width := 100;
          result.Height := 50;
          result.LoadFromStream(strm);

    finally
      strm.Free;
    end;
 end;

however when I put in listview this image the application hangs (stop responding..... use this way in listview

procedure TFrmprincipal.ExecuteREST(aList: TListview; aArray: string);
var
  obj : TjsonObject;
  Item : TlistviewItem;
  lResult : TjsonArray;
   strm : TMemoryStream;

begin
  lResult := REST(aArray);
  TThread.CreateAnonymousThread(
  procedure
  var
    i: Integer;
  begin
   for i:= 0 to lresult.Count-1 do
    begin
      TThread.Synchronize(TThread.CurrentThread, procedure()
      begin
         obj            := lResult.Items[i] as TJSONObject;
         Item           := aList.Items.Add;
         item.text      := obj.GetValue('nome').Value;
         if (obj.GetValue('logo').ToString <> 'null') then
             Item.bitmap := LerImage(''+PathUrl+'/images/empresas/'+obj.GetValue('logo').Value)
          else
          Item.bitmap := LerImage(''+PathUrl+'/images/empresas/semimagem.png');
      end);
     end
   end).Start;

end;

The detail is if put only 2 listview works correctly... but if you put a button to pull the other listview hangs also.... I’ve tried it in many ways, with Ttask, with Thread and without Thread, with Synchronize, with Queue and nothing.....

Someone gives me a light how to solve this??

  • In fact at first glance it is doing everything right.... no clue on the exact line where it hangs?

  • if I put image item.bitmap, this image comes from a website... if I place the three listview hangs, if puts two or one, opens normal.

1 answer

0

And if you change the concept?

Instead of creating a thread to do EVERYTHING, create a thread for each need?

Something like:

  for i := 0 to Pred(lResult.Count) do
  begin
    TThread.CreateAnonymousThread(
      procedure
      begin
        TThread.Synchronize(TThread.CurrentThread,
          procedure
          begin
            obj := lResult.Items[i] as TJSONObject;
            Item := aList.Items.Add;
            Item.text := obj.GetValue('nome').Value;

            if (obj.GetValue('logo').ToString <> 'null') then
              Item.bitmap := LerImage('' + PathUrl + '/images/empresas/' + obj.GetValue('logo').Value)
            else
              Item.bitmap := LerImage('' + PathUrl + '/images/empresas/semimagem.png');
          end);
      end).Start;
  end;

I just took the repetition from inside the thread, so each image will be rendered independently.

Another modification that is necessary is in function LerImage. Instead of just being one try finally consider using a try except, because, if it is an internet connection, for some reason a failure occurs the application will not stop.

Edit.

Perhaps you can still remove the TThread.Synchronize(TThread.CurrentThread,... is worth testing if not resolved with the proposed change.

Edit².

Now you need to change the listview property to display images. Right click on it and enable the Design Mode and change the property Appearance.

inserir a descrição da imagem aqui

By default it is defined as ListItem, just change to ImageListItem and rotate.

  • I did all this... putting the thread as suggested, does not lock, but the listview is all blank, nothing appears. put a showmessage in the array, and the data returns from the normal Rest.

  • @Geraldolacerda Take a look at the second edition of the answer. Wanted to modify the appearance property of Listview

  • the listview is all right, because if putting two listview works right, the detail is only when I put 3 listview... ex: listviewCompany and listviewdestaques, so opens certim does not lock or anything. Now add the listviewCategories, then lock everything or leave the opening screen

Browser other questions tagged

You are not signed in. Login or sign up in order to post.