How to cancel download with SDWebImageDownloader

2019-08-14 21:41发布

I'm using SDWebImageDownloader to download an image and then show it on a cell, but I have the issue that if the image doesn't finish download once the view is unloaded, it causes that my app crashes.

I'm using the code that is provided on the github page:

[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
                                                options:0
                                               progress:^(NSInteger receivedSize, NSInteger expectedSize)
                                               {
                                                   // progression tracking code
                                               }
                                               completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
                                               {
                                                   if (image && finished)
                                                   {
                                                       // do something with image
                                                   }
                                               }];

And in this github issue, a user says that this method returns a SDWebImageOperation, which can be cancelled, but I couldn't find a way to do cancel all the operations on the shared downloader.

1条回答
老娘就宠你
2楼-- · 2019-08-14 22:14

You may want to use an instance of SDWebImageManager instead, which includes methods for cancelling requests.

If you're showing the image in a UIImageView in a cell, however, you should just use the UIImageView (WebCache) class which extends UIImageView and allows you to cancel images you're loading with the method - (void)cancelCurrentImageLoad;.

You should use that method in UITableViewCell's prepareForReuse method.

查看更多
登录 后发表回答