Skip to content

Commit

Permalink
Apply compression quality to cache serializer and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Dec 17, 2019
1 parent 09875e9 commit c000659
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Sources/Cache/CacheSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,16 @@ public struct DefaultCacheSerializer: CacheSerializer {
/// If `original` is `nil`, the input `image` will be encoded as PNG data.
public func data(with image: KFCrossPlatformImage, original: Data?) -> Data? {
if preferCacheOriginalData {
return original ?? image.kf.data(format: original?.kf.imageFormat ?? .unknown)
return original ??
image.kf.data(
format: original?.kf.imageFormat ?? .unknown,
compressionQuality: compressionQuality
)
} else {
return image.kf.data(format: original?.kf.imageFormat ?? .unknown)
return image.kf.data(
format: original?.kf.imageFormat ?? .unknown,
compressionQuality: compressionQuality
)
}
}

Expand Down
51 changes: 51 additions & 0 deletions Tests/KingfisherTests/KingfisherManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,57 @@ class KingfisherManagerTests: XCTestCase {
coordinator.apply(.cachingOriginalImage) { called = true }
XCTAssertEqual(coordinator.state, .done)
}

func testCanUseCustomizeDefaultCacheSerializer() {
let exp = expectation(description: #function)
let url = testURLs[0]

var cacheSerializer = DefaultCacheSerializer()
cacheSerializer.preferCacheOriginalData = true

manager.cache.store(
testImage,
original: testImageData,
forKey: url.cacheKey,
processorIdentifier: DefaultImageProcessor.default.identifier,
cacheSerializer: cacheSerializer, toDisk: true) {
result in

let computedKey = url.cacheKey.computedKey(with: DefaultImageProcessor.default.identifier)
let fileURL = self.manager.cache.diskStorage.cacheFileURL(forKey: computedKey)
let data = try! Data(contentsOf: fileURL)
XCTAssertEqual(data, testImageData)

exp.fulfill()
}
waitForExpectations(timeout: 1.0)
}

func testCanUseCustomizeDefaultCacheSerializerStoreEncoded() {
let exp = expectation(description: #function)
let url = testURLs[0]

var cacheSerializer = DefaultCacheSerializer()
cacheSerializer.compressionQuality = 0.8

manager.cache.store(
testImage,
original: testImageJEPGData,
forKey: url.cacheKey,
processorIdentifier: DefaultImageProcessor.default.identifier,
cacheSerializer: cacheSerializer, toDisk: true) {
result in

let computedKey = url.cacheKey.computedKey(with: DefaultImageProcessor.default.identifier)
let fileURL = self.manager.cache.diskStorage.cacheFileURL(forKey: computedKey)
let data = try! Data(contentsOf: fileURL)
XCTAssertNotEqual(data, testImageJEPGData)
XCTAssertEqual(data, testImage.jpegData(compressionQuality: 0.8))

exp.fulfill()
}
waitForExpectations(timeout: 1.0)
}
}

class SimpleProcessor: ImageProcessor {
Expand Down

0 comments on commit c000659

Please sign in to comment.