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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top