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

Error image with custom Placeholder fix #1925

Merged
merged 7 commits into from
Apr 7, 2022
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
1 change: 1 addition & 0 deletions Sources/Extensions/ImageView+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {

case .failure:
if let image = options.onFailureImage {
mutatingSelf.placeholder = nil
self.base.image = image
}
completionHandler?(result)
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftUI/ImageBinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ extension KFImage {
guard let source = context.source else {
CallbackQueue.mainCurrentOrAsync.execute {
context.onFailureDelegate.call(KingfisherError.imageSettingError(reason: .emptySource))
if let image = context.options.onFailureImage {
self.loadedImage = image
}
self.loading = false
self.loaded = true
Comment on lines 55 to +61

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to pass weak or unowned self here?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. It is not necessary.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

}
return
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/KingfisherTests/ImageViewExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,28 @@ class ImageViewExtensionTests: XCTestCase {

waitForExpectations(timeout: 3, handler: nil)
}

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

stub(url, errorCode: 404)

let view = KFCrossPlatformView()

imageView.kf.setImage(
with: url,
placeholder: view,
options: [.onFailureImage(testImage)])
{
result in
XCTAssertEqual(self.imageView.image, testImage)
XCTAssertFalse(self.imageView.subviews.contains(view))
exp.fulfill()
}

waitForExpectations(timeout: 3, handler: nil)
}

func testSettingNonWorkingImageWithFailureImage() {
let exp = expectation(description: #function)
Expand Down