Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

1 MONTH VIP - 5$; 3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
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 14, 2014.

  1. AdminDF
    Online

    AdminDFAdminDF is a Verified Member DelphiFan Administrator 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.

    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.
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block