pinterest / PINRemoteImage

A thread safe, performant, feature rich image fetcher
Apache License 2.0
4.01k stars 511 forks source link

Load another image to a UIImageView after one image download has been completed #601

Open ryuta0201 opened 3 years ago

ryuta0201 commented 3 years ago

Hi, I'm trying to create a function as an extension of UIImageView that loads non-progressive image progressively. The URL passed to the function provides a progressive PNG image, which seems unable to load progressively even with self.pin_updateWithProgress option being true.

I changed the server's implementation so that the server returns low resolution image when /128 is added to the end of the image's URL. Then I'm trying to first load that low resolution image ("/128" one) into a UIImageView, followed by another pin_setImage which loads the final high resolution image to the UIImageView, using the low resolution image as placeHolder.

The code looks like the following:

    func setProgressively(url: URL, completion: @escaping (Bool) -> Void) {
        guard let url128 = URL(string: "\(url.absoluteString)/128") else { return }

        self.pin_setImage(from: url128) { [weak self] (result) in
            self?.clear()
            self?.pin_setImage(from: url, placeholderImage: result.image, completion: { [weak self] (finalresult) in
                PINRemoteImageManager.shared().cache.removeObject(forKey: PINRemoteImageManager.shared().cacheKey(for: url128, processorKey: nil))
                self?.pin_updateUI(with: finalresult)
            })
            completion(true)
        }
    }

    func clear() {
        pin_cancelImageDownload()
        pin_clearImages()
    }

However, the UIImageView doesn't update from the low resolution image, or it only loads the high resolution image for some occasions. Has anybody tried the similar implementation and knows a better practice?