Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions SKPhotoBrowser/SKPhoto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ open class SKPhoto: NSObject, SKPhotoProtocol {
open var contentMode: UIView.ContentMode = .scaleAspectFill
open var shouldCachePhotoURLImage: Bool = false
open var photoURL: String!
open var cacheKey: String!

override init() {
super.init()
Expand All @@ -38,19 +39,21 @@ open class SKPhoto: NSObject, SKPhotoProtocol {
underlyingImage = image
}

convenience init(url: String) {
convenience init(url: String, cacheKey: String?) {
self.init()
photoURL = url
self.cacheKey = cacheKey ?? url
}

convenience init(url: String, holder: UIImage?) {
convenience init(url: String, holder: UIImage?, cacheKey: String?) {
self.init()
photoURL = url
underlyingImage = holder
self.cacheKey = cacheKey ?? url
}

open func checkCache() {
guard let photoURL = photoURL else {
guard let photoURL = photoURL, let cacheKey = self.cacheKey else {
return
}
guard shouldCachePhotoURLImage else {
Expand All @@ -63,14 +66,14 @@ open class SKPhoto: NSObject, SKPhotoProtocol {
underlyingImage = img
}
} else {
if let img = SKCache.sharedCache.imageForKey(photoURL) {
if let img = SKCache.sharedCache.imageForKey(cacheKey) {
underlyingImage = img
}
}
}

open func loadUnderlyingImageAndNotify() {
guard photoURL != nil, let URL = URL(string: photoURL) else { return }
guard let photoURL = photoURL, let cacheKey = cacheKey, let URL = URL(string: photoURL) else { return }

if self.shouldCachePhotoURLImage {
if SKCache.sharedCache.imageCache is SKRequestResponseCacheable {
Expand All @@ -83,7 +86,7 @@ open class SKPhoto: NSObject, SKPhotoProtocol {
return
}
} else {
if let img = SKCache.sharedCache.imageForKey(photoURL) {
if let img = SKCache.sharedCache.imageForKey(cacheKey) {
DispatchQueue.main.async {
self.underlyingImage = img
self.loadUnderlyingImageComplete()
Expand Down Expand Up @@ -112,7 +115,7 @@ open class SKPhoto: NSObject, SKPhotoProtocol {
if SKCache.sharedCache.imageCache is SKRequestResponseCacheable {
SKCache.sharedCache.setImageData(data, response: response, request: task?.originalRequest)
} else {
SKCache.sharedCache.setImage(image, forKey: self.photoURL)
SKCache.sharedCache.setImage(image, forKey: cacheKey)
}
}
DispatchQueue.main.async {
Expand All @@ -138,11 +141,11 @@ extension SKPhoto {
return SKPhoto(image: image)
}

public static func photoWithImageURL(_ url: String) -> SKPhoto {
return SKPhoto(url: url)
public static func photoWithImageURL(_ url: String, cacheKey: String? = nil) -> SKPhoto {
return SKPhoto(url: url, cacheKey: cacheKey)
}

public static func photoWithImageURL(_ url: String, holder: UIImage?) -> SKPhoto {
return SKPhoto(url: url, holder: holder)
public static func photoWithImageURL(_ url: String, holder: UIImage?, cacheKey: String? = nil) -> SKPhoto {
return SKPhoto(url: url, holder: holder, cacheKey: cacheKey)
}
}
71 changes: 71 additions & 0 deletions SKPhotoBrowserTests/SKPhotoCacheTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// SKPhotoCacheTests.swift
//
//
// Created by fattiger00 on 2022/9/5.
//

import XCTest
@testable import SKPhotoBrowser

class SKPhotoCacheTests: XCTestCase {

let imageUrl = "https://placehold.jp/150x150.png"
static let cacheKey = "test_cache_key"

override func setUp() {
super.setUp()

}

override class func tearDown() {
super.tearDown()

SKCache.sharedCache.removeImageForKey(SKPhotoCacheTests.cacheKey)
}

func testPhotoDownloadable() throws {
// given
let photos = SKPhoto(
url: self.imageUrl,
cacheKey: SKPhotoCacheTests.cacheKey
)
photos.shouldCachePhotoURLImage = true

// when
photos.loadUnderlyingImageAndNotify()

// then
let expectation = self.expectation(forNotification: NSNotification.Name(rawValue: SKPHOTO_LOADING_DID_END_NOTIFICATION), object: photos, handler: nil)

self.wait(for: [expectation], timeout: 5)
}

func testPhotoCacheGetable() throws {
// given
let photos = SKPhoto(
url: self.imageUrl,
cacheKey: SKPhotoCacheTests.cacheKey
)
photos.shouldCachePhotoURLImage = true

//when
photos.checkCache()

//then
XCTAssertNotNil(photos.underlyingImage)
}

func testPhotoCached() throws {
let image = SKCache.sharedCache.imageForKey(SKPhotoCacheTests.cacheKey)
XCTAssertNotNil(image)
}

func testExample() throws {
try testPhotoDownloadable()
try testPhotoCacheGetable()
try testPhotoCached()
}


}