Kingfisher icon indicating copy to clipboard operation
Kingfisher copied to clipboard

Unable to cancel a task for LocalFileImageDataProvider

Open vilyever opened this issue 4 years ago • 1 comments

Check List

Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.

Issue Description

Unable to cancel a task for LocalFileImageDataProvider

What

while using KF to load a local image file into imageView, we found that sometimes the order of under code will cause unexpected result

imageView.kf.setImage
kf.cancelDownloadTask() <now kf task working in processingQueue>
imageView.image = someIcon
<after a short time(maybe 10ms),kf set image when process done>
now the imageView shows the wrong image

Reproduce

  • upload a image to device
  • add some process to options,e.g. DownsamplingImageProcessor
  • load image using kf
  • cancel kf
  • set image with imageView.image = sth

vilyever avatar May 26 '20 09:05 vilyever

The setImage would give you a DownloadTask, which represents a task related to image downloading. The same happens when you call cancelDownloadTask. However, in a LocalFileImageDataProvider, there is no url session downloading or any network things managed by Kingfisher, so you will never be able to cancel the image setting once it starts.

To avoid the wrong image being set, instead of setting the imageView.image, try to keep using Kingfisher's method to set it, even it is a plain image. This should give Kingfisher a chance to identify which is the correct image to be set.

// Rewrite the last step: set image with imageView.image = sth
let provider = RawImageDataProvider(data: image.pngData()!, cacheKey: "myCacheKey")
imageView.kf.setImage(with: provider)

onevcat avatar May 28 '20 14:05 onevcat