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

Avoid recreating animated image representations when reloaded with identical options #2347

Merged
merged 2 commits into from
Jan 29, 2025
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
4 changes: 2 additions & 2 deletions Sources/General/KingfisherManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ public class KingfisherManager: @unchecked Sendable {
// TODO: Optimize it when we can use async across all the project.
@Sendable func checkResultImageAndCallback(_ inputImage: KFCrossPlatformImage) {
var image = inputImage
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.
if image.kf.imageFrameCount != nil && image.kf.imageFrameCount != 1, options.imageCreatingOptions != image.kf.imageCreatingOptions, let data = image.kf.animatedImageData {
// Recreate animated image representation when loaded in different options.
// https://github.com/onevcat/Kingfisher/issues/1923
image = options.processor.process(item: .data(data), options: options) ?? .init()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Image/GIFAnimatedImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Foundation
import ImageIO

/// Represents a set of image creation options used in Kingfisher.
public struct ImageCreatingOptions {
public struct ImageCreatingOptions: Equatable {

/// The target scale of the image that needs to be created.
public var scale: CGFloat
Expand Down
9 changes: 9 additions & 0 deletions Sources/Image/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import UniformTypeIdentifiers
nonisolated(unsafe) private let animatedImageDataKey = malloc(1)!
nonisolated(unsafe) private let imageFrameCountKey = malloc(1)!
nonisolated(unsafe) private let imageSourceKey = malloc(1)!
nonisolated(unsafe) private let imageCreatingOptionsKey = malloc(1)!
#if os(macOS)
nonisolated(unsafe) private let imagesKey = malloc(1)!
nonisolated(unsafe) private let durationKey = malloc(1)!
Expand All @@ -55,6 +56,7 @@ nonisolated(unsafe) private let durationKey = malloc(1)!
private let animatedImageDataKey = malloc(1)!
private let imageFrameCountKey = malloc(1)!
private let imageSourceKey = malloc(1)!
private let imageCreatingOptionsKey = malloc(1)!
#if os(macOS)
private let imagesKey = malloc(1)!
private let durationKey = malloc(1)!
Expand All @@ -68,6 +70,11 @@ extension KingfisherWrapper where Base: KFCrossPlatformImage {
set { setRetainedAssociatedObject(base, animatedImageDataKey, newValue) }
}

private(set) var imageCreatingOptions: ImageCreatingOptions? {
get { return getAssociatedObject(base, imageCreatingOptionsKey) }
set { setRetainedAssociatedObject(base, imageCreatingOptionsKey, newValue) }
}

public var imageFrameCount: Int? {
get { return getAssociatedObject(base, imageFrameCountKey) }
set { setRetainedAssociatedObject(base, imageFrameCountKey, newValue) }
Expand Down Expand Up @@ -359,6 +366,7 @@ extension KingfisherWrapper where Base: KFCrossPlatformImage {
image?.kf.animatedImageData = source.data
image?.kf.imageFrameCount = source.frameCount
image?.kf.frameSource = source
image?.kf.imageCreatingOptions = options
return image
#else

Expand Down Expand Up @@ -390,6 +398,7 @@ extension KingfisherWrapper where Base: KFCrossPlatformImage {
}

image?.kf.imageFrameCount = source.frameCount
image?.kf.imageCreatingOptions = options
return image
#endif
}
Expand Down
19 changes: 19 additions & 0 deletions Tests/KingfisherTests/KingfisherManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,25 @@ class KingfisherManagerTests: XCTestCase {
waitForExpectations(timeout: 3, handler: nil)
}

func testAnimatedImageShouldNotRecreateWithSameOptions() {
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), .onlyLoadFirstFrame]) { result in
XCTAssertFalse(p.processed)
XCTAssertTrue(result.value!.image.creatingOptions!.onlyFirstFrame)
exp.fulfill()
}
}
waitForExpectations(timeout: 3, handler: nil)
}

func testMissingResourceOfLivePhotoFound() {
let resource = KF.ImageResource(downloadURL: LivePhotoURL.mov)
let source = LivePhotoSource(resources: [resource])
Expand Down