Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
fix cache bug for photo version update
Browse files Browse the repository at this point in the history
  • Loading branch information
Suric zhang committed Aug 14, 2017
1 parent 18a6170 commit 98068b7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
21 changes: 8 additions & 13 deletions PhotoBrowser/Photo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand All @@ -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
}
}
4 changes: 2 additions & 2 deletions PhotoBrowser/PhotoPreviewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ 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 {
newWaitingView.center = view.center
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 {
Expand Down
6 changes: 3 additions & 3 deletions PhotoBrowserDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 98068b7

Please sign in to comment.