Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always use specified processors to recreate animated image representation #2099

Merged
merged 2 commits into from
Jun 19, 2023
Merged
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
2 changes: 1 addition & 1 deletion Sources/General/KingfisherManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public class KingfisherManager {
if image.kf.imageFrameCount != nil && image.kf.imageFrameCount != 1, let data = image.kf.animatedImageData {
// Always recreate animated image representation since it is possible to be loaded in different options.
// https://github.com/onevcat/Kingfisher/issues/1923
image = KingfisherWrapper.animatedImage(data: data, options: options.imageCreatingOptions) ?? .init()
image = options.processor.process(item: .data(data), options: options) ?? .init()
}
if let modifier = options.imageModifier {
image = modifier.modify(image)
Expand Down
34 changes: 33 additions & 1 deletion Tests/KingfisherTests/KingfisherManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,35 @@ class KingfisherManagerTests: XCTestCase {

waitForExpectations(timeout: 3, handler: nil)
}

// https://github.com/onevcat/Kingfisher/issues/1923
func testAnimatedImageShouldRecreateFromCache() {
let exp = expectation(description: #function)
let url = testURLs[0]
let data = testImageGIFData
stub(url, data: data)
let p = SimpleProcessor()
manager.retrieveImage(with: url, options: [.processor(p), .onlyLoadFirstFrame]) { result in
XCTAssertTrue(p.processed)
XCTAssertTrue(result.value!.image.creatingOptions!.onlyFirstFrame)
p.processed = false
self.manager.retrieveImage(with: url, options: [.processor(p)]) { result in
XCTAssertTrue(p.processed)
XCTAssertFalse(result.value!.image.creatingOptions!.onlyFirstFrame)
exp.fulfill()
}
}
waitForExpectations(timeout: 3, handler: nil)
}
}

private var imageCreatingOptionsKey: Void?

extension KFCrossPlatformImage {
var creatingOptions: ImageCreatingOptions? {
get { return getAssociatedObject(self, &imageCreatingOptionsKey) }
set { setRetainedAssociatedObject(self, &imageCreatingOptionsKey, newValue) }
}
}

class SimpleProcessor: ImageProcessor {
Expand All @@ -1208,7 +1237,10 @@ class SimpleProcessor: ImageProcessor {
case .image(let image):
return image
case .data(let data):
return KingfisherWrapper<KFCrossPlatformImage>.image(data: data, options: options.imageCreatingOptions)
let creatingOptions = options.imageCreatingOptions
let image = KingfisherWrapper<KFCrossPlatformImage>.image(data: data, options: creatingOptions)
image?.creatingOptions = creatingOptions
return image
}
}
}
Expand Down