我如何保存图像并稍后在 Idhttp 中使用它们

how do i save image and use them later in Idhttp

通过跟进我之前关于更新的问题 listview

我开始以不同的方式思考以解决我之前的问题,因为下载过程花费的时间太长并且占用了太多带宽我想下载 GIFimage 然后将其保存在磁盘上然后稍后在我的内部使用它应用 这是当前下载线程代码

procedure TDownloadImage.Execute;
var
  aMs: TMemoryStream;
  aIdHttp: TIdHttp;
begin
  FGif := TGifImage.Create;
  try
    aMs := TMemoryStream.Create;
    try
      aIdHttp := TIdHttp.Create(nil);
      try
        aIdHttp.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
        aIdHttp.Get(FURL, aMs); // here is the client FURL
      finally
        aIdHttp.Free;
      end;
      aMs.Position := 0;
      FGif.LoadFromStream(aMs);
      FGif.Transparent := True;
    finally
      aMs.Free;
    end;
    if Assigned(FOnImageReady) then
      Synchronize(DoImageReady);
    end;
  finally
    FGif.Free;
  end;
end;

我想在计算机客户端上保存 FURL 的图像,然后如果请求再次下载此图像,请中止下载过程并从客户端计算机加载它,我怎么可能这样做?

正如 TLama 明智地说的那样,您正在寻找 Full Cache 机制。在 Delphi 中,您可能希望将其称为 TDictionary。

如果您想将 Gif 和 URI 保存在内存中,那么您可以使用一对 TDictionary 来保存每个内存流和 uri,一切都会很快。

如果你想将数据存储在文件上,并且每次应用程序开始记住前一个运行的状态,你可能想找到一种方法来存储数据、数据库或文本文件。

如果您想要一个可以提供帮助的简单文本文件,Ini 文件可以完成这项工作,但是,随着文本文件的增长,它往往会变慢,并使搜索变慢,然后文件获取。

如果你使用一个好的便携式数据库,也许搜索会很快。

带 Ini 文件的示例代码可帮助您入门: 伪代码。

var 
  filename:string;//where is the key value is stored.
  GifLocation:string;//result of the cache hit.
const
  URI = 'http://sstatic.net/stackexchange/img/logos/so/so-logo-med.png?v=a4a65015804e';
begin
  with TIniFile.create(filename) do
  try
    GifLocation := readString('FilesLocation',URI,'');
  finally
    free;
  end;
  if ('' = GifLocation) then 
    fetchBoy(URI)
  else IGotIt_IGotIt(GifLocation);
end;

实施您的 fetchBoy 以执行 http 获取,并 IGotIt_IGotIt 加载文件流。