From 98068b7e200e19315fcc33545586749546104e21 Mon Sep 17 00:00:00 2001 From: Suric zhang <372081349@qq.com> Date: Mon, 14 Aug 2017 11:39:59 +0800 Subject: [PATCH] fix cache bug for photo version update --- PhotoBrowser/Photo.swift | 21 ++++++++------------- PhotoBrowser/PhotoPreviewController.swift | 4 ++-- PhotoBrowserDemo/ViewController.swift | 6 +++--- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/PhotoBrowser/Photo.swift b/PhotoBrowser/Photo.swift index 0545e15..e8c3638 100644 --- a/PhotoBrowser/Photo.swift +++ b/PhotoBrowser/Photo.swift @@ -19,14 +19,16 @@ public struct Photo { public var title: String? public var object: AnyObject? public var asset: PHAsset? + public var fileKey: String? - public init(image: UIImage?, title: String? = nil, thumbnailImage: UIImage? = nil, photoUrl: URL? = nil, thumbnailUrl: URL? = nil, object: AnyObject? = nil) { + public init(image: UIImage?, title: String? = nil, thumbnailImage: UIImage? = nil, photoUrl: URL? = nil, thumbnailUrl: URL? = nil, object: AnyObject? = nil, fileKey: String?) { self.image = image self.title = title self.thumbnailImage = thumbnailImage self.photoUrl = photoUrl self.thumbnailUrl = thumbnailUrl self.object = object + self.fileKey = fileKey } public init(asset: PHAsset) { @@ -36,9 +38,12 @@ public struct Photo { public func localOriginalPhoto() -> UIImage? { if image != nil { return image + } else if let originFileKey = fileKey { + let image = KingfisherManager.shared.cache.retrieveImageInMemoryCache(forKey: originFileKey) + return image ?? KingfisherManager.shared.cache.retrieveImageInDiskCache(forKey: originFileKey) } else if let photoUrl = photoUrl { - let image = KingfisherManager.shared.cache.retrieveImageInMemoryCache(forKey: photoUrl.kfCacheKey) - return image ?? KingfisherManager.shared.cache.retrieveImageInDiskCache(forKey: photoUrl.kfCacheKey) + let image = KingfisherManager.shared.cache.retrieveImageInMemoryCache(forKey: photoUrl.absoluteString) + return image ?? KingfisherManager.shared.cache.retrieveImageInDiskCache(forKey: photoUrl.absoluteString) } return nil } @@ -65,13 +70,3 @@ 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 0e1760e..9c86c33 100644 --- a/PhotoBrowser/PhotoPreviewController.swift +++ b/PhotoBrowser/PhotoPreviewController.swift @@ -163,7 +163,7 @@ class PhotoPreviewController: UIViewController { waitingView.removeFromSuperview() } - if let photoUrl = photo.photoUrl { + if let photoUrl = photo.photoUrl, let photoFileKey = photo.fileKey { waitingView = WaitingView.init(frame: CGRect(x: 0, y: 0, width: 70, height: 70)) if let newWaitingView = waitingView { @@ -171,7 +171,7 @@ class PhotoPreviewController: UIViewController { view.addSubview(newWaitingView) } - let resource = ImageResource(downloadURL: photoUrl, cacheKey: photoUrl.kfCacheKey) + let resource = ImageResource(downloadURL: photoUrl, cacheKey: photoFileKey) 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 { diff --git a/PhotoBrowserDemo/ViewController.swift b/PhotoBrowserDemo/ViewController.swift index b6edde5..a8f0757 100644 --- a/PhotoBrowserDemo/ViewController.swift +++ b/PhotoBrowserDemo/ViewController.swift @@ -47,9 +47,9 @@ extension ViewController { let thumbnail3 = UIImage.init(named: "thumbnail3") let photoUrl3 = URL.init(string: "https://pic2.zhimg.com/a5455838750e168d97480d9247537d31_r.jpeg") - let photo = Photo.init(image: nil, title:"Image1fjdkkfadjfkajdkfalkdsfjklasfklaskdfkadsjfklajklsdjfkajsdkfaksdjfkajsdkfjlaksdfjkakdfklak", thumbnailImage: thumbnail1, photoUrl: photoUrl1) - let photo2 = Photo.init(image: nil, title:"Image2", thumbnailImage: thumbnail2, photoUrl: photoUrl2) - let photo3 = Photo.init(image: nil, title:"Image3", thumbnailImage: thumbnail3, photoUrl: photoUrl3) + let photo = Photo.init(image: nil, title:"Image1fjdkkfadjfkajdkfalkdsfjklasfklaskdfkadsjfklajklsdjfkajsdkfaksdjfkajsdkfjlaksdfjkakdfklak", thumbnailImage: thumbnail1, photoUrl: photoUrl1, fileKey: "abc100") + let photo2 = Photo.init(image: nil, title:"Image2", thumbnailImage: thumbnail2, photoUrl: photoUrl2, fileKey: "abc101") + let photo3 = Photo.init(image: nil, title:"Image3", thumbnailImage: thumbnail3, photoUrl: photoUrl3, fileKey: "abc102") let item1 = PBActionBarItem(title: "ONE", style: .plain) { (photoBrowser, item) in let photos = [photo, photo2]