From bc5cbab381f47e403abfd41f05a980abf887c521 Mon Sep 17 00:00:00 2001 From: Suric zhang <372081349@qq.com> Date: Fri, 16 Dec 2016 19:31:33 +0800 Subject: [PATCH] set url without query as cachekey --- PhotoBrowser/Photo.swift | 14 ++++++++++++-- PhotoBrowser/PhotoPreviewController.swift | 4 +++- PhotoBrowserDemo/ViewController.swift | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/PhotoBrowser/Photo.swift b/PhotoBrowser/Photo.swift index b8fa29b..0545e15 100644 --- a/PhotoBrowser/Photo.swift +++ b/PhotoBrowser/Photo.swift @@ -37,8 +37,8 @@ public struct Photo { if image != nil { return image } else if let photoUrl = photoUrl { - let image = KingfisherManager.shared.cache.retrieveImageInMemoryCache(forKey: photoUrl.absoluteString) - return image ?? KingfisherManager.shared.cache.retrieveImageInDiskCache(forKey: photoUrl.absoluteString) + let image = KingfisherManager.shared.cache.retrieveImageInMemoryCache(forKey: photoUrl.kfCacheKey) + return image ?? KingfisherManager.shared.cache.retrieveImageInDiskCache(forKey: photoUrl.kfCacheKey) } return nil } @@ -65,3 +65,13 @@ public struct Photo { } } + +extension URL { + var kfCacheKey: String { + var photoCacheKey = absoluteString + if let query = query { + photoCacheKey = absoluteString.replacingOccurrences(of: query, with: "") + } + return photoCacheKey + } +} diff --git a/PhotoBrowser/PhotoPreviewController.swift b/PhotoBrowser/PhotoPreviewController.swift index e221605..6d4fd66 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -131,7 +131,9 @@ class PhotoPreviewController: UIViewController { newWaitingView.center = view.center view.addSubview(newWaitingView) } - imageView.kf.setImage(with: photoUrl, placeholder: photo.localThumbnailPhoto(), options: nil, progressBlock: { (receivedSize, totalSize) -> () in + + let resource = ImageResource(downloadURL: photoUrl, cacheKey: photoUrl.kfCacheKey) + imageView.kf.setImage(with: resource, placeholder: photo.localThumbnailPhoto(), options: nil, progressBlock: { (receivedSize, totalSize) -> () in let progress = CGFloat(receivedSize) / CGFloat(totalSize) if let waitingView = self.waitingView { waitingView.progress = progress diff --git a/PhotoBrowserDemo/ViewController.swift b/PhotoBrowserDemo/ViewController.swift index bd6b458..49f92ab 100644 --- a/PhotoBrowserDemo/ViewController.swift +++ b/PhotoBrowserDemo/ViewController.swift @@ -41,7 +41,7 @@ class ViewController: UIViewController { extension ViewController { func displayPhotoBrowser() { let thumbnail1 = UIImage.init(named: "thumbnail1") - let photoUrl1 = URL.init(string: "https://pic4.zhimg.com/453d7ebcdb0c4494e60fa07d09a83a83_r.jpeg") + let photoUrl1 = URL.init(string: "https://www.teambition.com/api/works/575d0bbf93e0f8860d73d88e/download/IMG_0005.JPG.jpeg?signature=eyJhbGciOiJIUzI1NiJ9.eyJfd29ya0lkIjoiNTc1ZDBiYmY5M2UwZjg4NjBkNzNkODhlIiwiZmlsZUtleSI6IjExMGg2MzRhOTg3MDQ5MDJmNDY1MmJhZTYzYjI1MTQyZTUyMSIsIl91c2VySWQiOiI1MmE2Y2MyZGVmNjZiYzk4MGMwMDAzMTIiLCJleHAiOjE0ODIwNjA0ODgsInN0b3JhZ2UiOiJzdHJpa2VyLWh6In0.czjEgDdAKqqOm8fK8lDOiE7wA_5H48bh6phlomGQqWo") let thumbnail2 = UIImage.init(named: "thumbnail2") let photoUrl2 = URL.init(string: "https://pic1.zhimg.com/0f70807392a9f62528b00ec434f5519c_b.png") let thumbnail3 = UIImage.init(named: "thumbnail3")