【问题标题】:How to download web images in Cache before displaying using SDWebimage?使用 SDWebimage 显示之前如何在缓存中下载 Web 图像?
【发布时间】:2013-07-29 02:39:38
【问题描述】:

我必须在我的应用程序中下载大量网络图像。现在我正在尝试使用 Grand Central Dispatch 在我的第一堂课中下载图像。

代码:

- (void)viewDidLoad
{
[super viewDidLoad];

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
    dispatch_async(queue, ^{
    // Perform non main thread operation
    for (NSString *imageURL in tempoaryArr1) {
        NSURL *url = [NSURL URLWithString:imageURL];

        SDWebImageManager *manager = [SDWebImageManager sharedManager];

        [manager downloadWithURL:url
                        delegate:self
                         options:0
                         success:^(UIImage *image, BOOL cached)
         {

         }
                         failure:nil];


    }
    dispatch_sync(dispatch_get_main_queue(), ^{
        // perform main thread operation
    });
});
}

但上面的代码只在应用程序第一次启动时下载 30 到 40 张图像,之后它停止在缓存中下载图像,直到我停止应用程序并再次运行它,然后第二次在缓存中下载更多图像,之后停止下载等等。

所以我的问题是为什么它在下载一些图像后停止,我希望它继续在缓存中下载图像,直到它下载所有图像。

【问题讨论】:

    标签: iphone ios caching grand-central-dispatch sdwebimage


    【解决方案1】:

    因此,您在视图控制器中进行此下载(正如您将其放入“viewDidLoad”方法中所证明的那样)。

    可能发生的情况是,当您移动到另一个视图时,或者当视图控制器从内存中释放时,您在该 dispatch_queue 中使用的“manager”对象也会在视图控制器被释放时也在留下记忆。

    这就是下载停止的原因。

    为了证明我是对的,请将正在执行所有这些 GCD 工作的视图留在屏幕上。我打赌你会得到所有你的图片,而不仅仅是 30 或 40。

    您需要做的是将此代码移动到它可以保存的位置,直到它完成下载(也许不是视图控制器,它只在用户想要看到它的时候显示在屏幕上)?

    【讨论】:

      【解决方案2】:
      SDWebImageManager *manager = [SDWebImageManager sharedManager];
      
      [manager downloadImageWithURL:Imageurl options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize)
      
      {
      
      
      } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
      
          if(image){
              NSLog(@"image=====%@",image);
          }
      }]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-19
        • 1970-01-01
        • 2012-10-13
        • 1970-01-01
        • 2015-09-09
        • 2014-04-15
        • 2023-04-03
        • 1970-01-01
        相关资源
        最近更新 更多