【问题标题】:how to store url image in cache memory then display in imageview in iphone如何将url图像存储在缓存中,然后在iphone的imageview中显示
【发布时间】:2013-03-08 10:34:22
【问题描述】:

我直接在 Imageview 中打开 URL 图像,但我想将图像存储在缓存中以供将来参考...帮我解决我的问题..

提前致谢

【问题讨论】:

  • 到目前为止,你写了什么代码(用于保存图像)?在此处发布。

标签: iphone ios ios4


【解决方案1】:

您可以使用使用图像缓存的SDWebImage...

这个库为UIImageVIew 提供了一个类别,支持来自网络的远程图像。

它提供:

一个UIImageView 类别将网络图像和缓存管理添加到 Cocoa Touch 框架中

一个异步图片下载器

具有自动缓存过期处理的异步内存 + 磁盘映像缓存

背景图片解压

保证不会多次下载同一个网址

保证不会一次又一次地重试虚假网址

保证主线程永远不会被阻塞

表演! 使用 GCD 和 ARC

【讨论】:

  • mac osx 的任何类似的东西
  • 也可以试试 SmartStorage(github.com/Alterplay/APSmartStorage)。它会自动将 UIImage/NSData 存储在磁盘或内存中。很聪明。
【解决方案2】:

这是iOS开发中的经典问题,我之前写了一个类OnlineImageView,使用的是NSOperationQueue -- 现在GCD可能更好。

您想访问在线图片:

  1. 首先,您尝试在 内存缓存,但错过了。
  2. 其次,您尝试在磁盘中查找图像,再次错过。
  3. 然后,您应该使用 GCD 异步下载在线图像 或其他一些方法。
  4. 图片下载成功后,显示在 UIImageView,并将其缓存在内存中,将其写入磁盘 以备将来使用。

一些示例代码:

- (void)downloadImage:(NSDictionary *)info
{
    /* HOWTO: Prevent Downloading the same url many times */
    /* Keep the download task one by one ? */

    NSString *url = [info objectForKey:@"url"];
    NSURL *imageUrl = [NSURL URLWithString:url];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];

    if (image) {
        id theDelegate = [info objectForKey:@"delegate"];
        if ([theDelegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)]) {
            [theDelegate imageDownloader:self didFinishWithImage:image];
        }

        [cacheQueue cacheImage:image withKey:url];
    } else {
#ifdef DEBUG
        NSLog(@"Failed to download : %@\n", url);
#endif
    }
}

【讨论】:

    【解决方案3】:

    使用 SDWebImage 。它最好和最容易的图像数据缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      相关资源
      最近更新 更多