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

Add ImageFilter support to UIButton extension #129

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 9 additions & 2 deletions Source/UIButton+AlamofireImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ extension UIButton {
/// to `nil`.
/// - parameter progress: The closure to be executed periodically during the lifecycle of the request.
/// Defaults to `nil`.
/// - parameter filter: The image filter applied to the image after the image request is
/// finished. Defaults to `nil`.
/// - parameter progressQueue: The dispatch queue to call the progress closure on. Defaults to the main queue.
/// - parameter completion: A closure to be executed when the image request finishes. The closure takes a
/// single response value containing either the image or the error that occurred. If
Expand All @@ -123,6 +125,7 @@ extension UIButton {
for state: UIControlState,
url: URL,
placeHolderImage: UIImage? = nil,
filter: ImageFilter? = nil,
progress: ImageDownloader.ProgressHandler? = nil,
progressQueue: DispatchQueue = DispatchQueue.main,
completion: ((DataResponse<UIImage>) -> Void)? = nil)
Expand All @@ -131,6 +134,7 @@ extension UIButton {
for: state,
urlRequest: urlRequest(with: url),
placeholderImage: placeHolderImage,
filter: filter,
progress: progress,
progressQueue: progressQueue,
completion: completion
Expand All @@ -149,6 +153,8 @@ extension UIButton {
/// to `nil`.
/// - parameter progress: The closure to be executed periodically during the lifecycle of the request.
/// Defaults to `nil`.
/// - parameter filter: The image filter applied to the image after the image request is
/// finished. Defaults to `nil`.
/// - parameter progressQueue: The dispatch queue to call the progress closure on. Defaults to the main queue.
/// - parameter completion: A closure to be executed when the image request finishes. The closure takes a
/// single response value containing either the image or the error that occurred. If
Expand All @@ -158,6 +164,7 @@ extension UIButton {
for state: UIControlState,
urlRequest: URLRequestConvertible,
placeholderImage: UIImage? = nil,
filter: ImageFilter? = nil,
progress: ImageDownloader.ProgressHandler? = nil,
progressQueue: DispatchQueue = DispatchQueue.main,
completion: ((DataResponse<UIImage>) -> Void)? = nil)
Expand All @@ -172,7 +179,7 @@ extension UIButton {
// Use the image from the image cache if it exists
if
let request = urlRequest.urlRequest,
let image = imageCache?.image(for: request, withIdentifier: nil)
let image = imageCache?.image(for: request, withIdentifier: filter?.identifier)
{
let response = DataResponse<UIImage>(
request: urlRequest.urlRequest,
Expand All @@ -197,7 +204,7 @@ extension UIButton {
let requestReceipt = imageDownloader.download(
urlRequest,
receiptID: downloadID,
filter: nil,
filter: filter,
progress: progress,
progressQueue: progressQueue,
completion: { [weak self] response in
Expand Down
50 changes: 50 additions & 0 deletions Tests/UIButtonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,28 @@ class UIButtonTests: BaseTestCase {
XCTAssertTrue(secondEqualityCheck, "second equality check should be true")
}

func testThatImageCanBeLoadedFromImageCacheFromRequestAndFilterIdentifierIfAvailable() {
// Given
let button = UIButton()

let downloader = ImageDownloader.default
let download = try! URLRequest(url: url.absoluteString, method: .get)
let expectation = self.expectation(description: "image download should succeed")

downloader.download(download, filter: CircleFilter()) { (_) in
expectation.fulfill()
}

waitForExpectations(timeout: timeout, handler: nil)

// When
button.af_setImage(for: .normal, url: url, filter: CircleFilter())
button.af_cancelImageRequest(for: .normal)

// Then
XCTAssertNotNil(button.image(for: .normal), "image view image should not be nil")
}

// MARK: - Placeholder Images

func testThatPlaceholderImageIsDisplayedUntilImageIsDownloadedFromURL() {
Expand Down Expand Up @@ -491,6 +513,34 @@ class UIButtonTests: BaseTestCase {
XCTAssertFalse(button.backgroundImage(for:[]) === placeholderImage, "button background image should not equal placeholder image")
}

// MARK: - Image Filters

func testThatImageFilterCanBeAppliedToDownloadedImageBeforeBeingDisplayed() {
// Given
let size = CGSize(width: 20, height: 20)
let filter = ScaledToSizeFilter(size: size)

let expectation = self.expectation(description: "image download should succeed")
var imageDownloadComplete = false

let button = TestButton {
imageDownloadComplete = true
expectation.fulfill()
}

// When
button.af_setImage(for: .normal, url: url, filter: filter)
waitForExpectations(timeout: timeout, handler: nil)

// Then
XCTAssertTrue(imageDownloadComplete, "image download complete should be true")
XCTAssertNotNil(button.image(for: .normal), "image view image should not be nil")

if let image = button.image(for: .normal) {
XCTAssertEqual(image.size, size, "image size does not match expected value")
}
}

// MARK: - Completion Handler

func testThatCompletionHandlerIsCalledWhenImageDownloadSucceeds() {
Expand Down