SDWebImage + MBProgressHUD

SDWebImage + MBProgressHUD

美好的一天!

我在从网上下载全尺寸图片时使用这两个库。

代码:

-(void)viewDidAppear:(BOOL)animated
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^
    {
        [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", img]] placeholderImage:[UIImage imageNamed:@"stub_image.jpg"]];

    dispatch_async(dispatch_get_main_queue(), ^
    {
      [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
});

事实证明,完整尺寸图片的加载时间晚于隐藏指示器(显示的占位符图像)

我做错了什么?

在您的代码中,行

[MBProgressHUD hideHUDForView:self.view animated:YES];

在下载完成之前执行。

方法sd_setImageWithURL:placeholderImage:非阻塞线程

您应该使用 sd_setImageWithURL:placeholderImage:completed:completionBlock 并将您的隐藏方法添加到其中。

Using blocks

With blocks, you can be notified about the image download progress and whenever the image retrieval has completed with success or not:

注意:如果您的图像请求在完成前被取消,则您的成功块和失败块都不会被调用。

试试这个代码:

-(void)viewDidAppear:(BOOL)animated
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
                [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", img]] placeholderImage:[UIImage imageNamed:@"stub_image.jpg"]
                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
                    {
                        dispatch_async(dispatch_get_main_queue(), ^ {
                            [MBProgressHUD hideHUDForView:self.view animated:YES];
                        });
        }];
    });
}