1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Donation with Paypal!!!

    Go to your paypal account and send directly donation to [email protected]com

    1 month - 10 $ - Standart VIP

    6 months - 20 $- Standart VIP

    1 year - 30 $- Standart VIP

    2 years - 50 $- Standart VIP

    Gold member for life - 150 $- Standart VIP

    High Vip (Standart VIP include) group please send PM or email to [email protected] for info

    After Donation please send email to [email protected]

  3. Donation Ways 2020


    Paysend
  4. Telegram
Dismiss Notice

Donation with Paypal!!!

Go to your paypal account and send directly donation to [email protected]

1 month - 10 $ - Standart VIP

6 months - 20 $- Standart VIP

1 year - 30 $- Standart VIP

2 years - 50 $- Standart VIP

Gold member for life - 150 $- Standart VIP

High Vip (Standart VIP include) group please send PM or email to [email protected] for info

After Donation please send email to [email protected]

Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

ImageLoader for FireMonkey TImage component

Discussion in 'Delphi Android And IOS' started by AdminDF, Mar 5, 2014.

  1. AdminDF
    Online

    AdminDFAdminDF is a Verified Member Delphifan Staff Member DF Staff

    A ImageLoader for FireMonkey TImage component. I written this when I found I need to load a few images from the Internet. Obviously delphi isn’s browser, if I just call download using Indy it will just bHIDE-THANKS my app’s UI.

    ImageLoader is very simple class with a loading queue, and a timer to trigger the work on the queue. You just need to call the LoadImage from TImageLoader, you don’t even need to create TImageLoader there is a default “DefaultImageLoader” already created on unit initialization. A simple cache machanism is also there to prevent waste of bandwidth.


    Code:
    DefaultImageLoader.LoadImage(
    Image1,
    'http://www.google.com/images/errors/robot.png'
    );
    

    What’s next

    Right now, ImageLoader only loads the image for TImage, it doesn’t fire any event when the image is ready, so post processing is not available. That will be added in the future. Ajax waiting animation will be added as option.

    Full Source:
    Code:
    unit uImageLoader;
     
    interface
     
    uses SysUtils, Classes, System.Generics.Collections,
       FMX.Types, FMX.Objects, FMX.Controls, AsyncTask, AsyncTask.HTTP;
     
    type
      TLoadQueueItem = record
        ImageURL: String;
        Image: TImage;
      end;
      TLoadQueue = TList<TLoadQueueItem>;
     
      TImageLoader = class(TObject)
      private
        fQueue: TLoadQueue;
        fWorker: TTimer;
        fActiveItem: TLoadQueueItem;
        fIsWorking: Boolean;
        procedure QueueWorkerOnTimer(ASender: TObject);
      public
        constructor Create;
        destructor Destroy; override;
     
        procedure LoadImage(AImage: TImage; AImageURL: string);
        property ActiveItem: TLoadQueueItem read fActiveItem;
        property IsWorking: Boolean read fIsWorking;
      end;
     
    var
      DefaultImageLoader: TImageLoader;
     
    implementation
     
    var
      FCachedImages: TObjectDictionary<String, TBitmap>;
    { TImageLoader }
     
    constructor TImageLoader.Create;
    begin
      inherited Create;
      fQueue := TLoadQueue.Create;
      fIsWorking := False;
      fWorker := TTimer.Create(nil);
      fWorker.Enabled := False;
      fWorker.Interval := 100;
      fWorker.OnTimer := QueueWorkerOnTimer;
      fWorker.Enabled := True;
    end;
     
    destructor TImageLoader.Destroy;
    begin
      fWorker.Free;
      fQueue.Free;
      inherited;
    end;
     
    procedure TImageLoader.LoadImage(AImage: TImage; AImageURL: string);
    var
      item: TLoadQueueItem;
    begin
      item.ImageURL := AImageURL;
      item.Image := AImage;
      fQueue.Add(item);
    end;
     
    procedure TImageLoader.QueueWorkerOnTimer(ASender: TObject);
    var
      lBitmap: TBitmap;
    begin
      fWorker.Enabled := False;
      if (fQueue.Count > 0) and (not fIsWorking) then
      begin
        fIsWorking := True;
        fActiveItem := fQueue[0];
        fQueue.Delete(0);
        lBitmap := nil;
        if FCachedImages.TryGetValue(fActiveItem.ImageURL, lBitmap) and (lBitmap <> nil) then
        begin
          fActiveItem.Image.Bitmap.Assign(lBitmap);
          fIsWorking := False;
        end else
        begin
          AsyncTask.Run(
            THttpAsyncTaskBitmap.Create(fActiveItem.ImageURL),
            // Finished
            procedure (ATask: IAsyncTask)
            var
              fBitmap: TBitmap;
            begin
              lBitmap := TBitmap.Create(0, 0);
              fBitmap := (ATask as IHttpBitmapResponse).Bitmap;
              if fBitmap <> nil then
              begin
                lBitmap.Assign(fBitmap);
                FCachedImages.AddOrSetValue(fActiveItem.ImageURL, lBitmap);
                fActiveItem.Image.Bitmap.Assign(lBitmap);
              end;
              fIsWorking := False;
            end
          );
        end;
     
      end;
      fWorker.Enabled := True;
    end;
     
    initialization
      FCachedImages := TObjectDictionary<String, TBitmap>.Create([], 10);
      DefaultImageLoader := TImageLoader.Create;
     
    finalization
      FCachedImages.Free;
      DefaultImageLoader.Free;
     
     
    end.
     
  2. woomob
    Offline

    woomob DF Member

     
  3. mato0o
    Offline

    mato0o DF Junior

    Thank you!
     
  4. haneol
    Online

    haneol Guest

    [quote pid=2463 dateline=1394004024]Thanks[/quote]
     
  5. jekl
    Offline

    jekl DF Junior

    RE:

    thanks
     
  6. sqlnew
    Offline

    sqlnew DF Member

    RE:

    thanks
     
  7. bz00
    Offline

    bz00 DF Junior

    RE:

    thanks
     
  8. hugoboss
    Offline

    hugoboss DF Junior

    thanks
     

Share This Page