1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- //
- // UIButton+Kingfisher.swift
- // SwiftBilibili
- //
- // Created by 罗文 on 2021/7/3.
- // Copyright © 2021年 罗文. All rights reserved.
- //
- import Foundation
- import Kingfisher
- extension UIButton {
-
- @discardableResult
- func setImage(
- with resource: Resource?,
- for state: UIControl.State,
- placeholder: UIImage? = nil,
- options: ImageOptions = [.transition(.fade(0.25))],
- progress: ((Int64, Int64) -> Void)? = nil,
- completion: ((ImageResult) -> Void)? = nil
- ) -> DownloadTask? {
- return self.kf.setImage(with: resource, for: state, placeholder: placeholder, options: options, progressBlock: progress, completionHandler: { result in
- switch result {
- case .success(let value):
- print("Task done for: \(value.source.url?.absoluteString ?? "")")
- completion?(.success(value.image))
- case .failure(let error):
- print("Job failed: \(error.localizedDescription)")
- completion?(.failure(error))
- }
- })
- }
-
- @discardableResult
- func setBackgroundImage(
- with resource: Resource?,
- for state: UIControl.State,
- placeholder: UIImage? = nil,
- options: ImageOptions = [.transition(.fade(0.25))],
- progress: ((Int64, Int64) -> Void)? = nil,
- completion: ((ImageResult) -> Void)? = nil
- ) -> DownloadTask? {
- return self.kf.setBackgroundImage(with: resource, for: state, placeholder: placeholder, options: options, progressBlock: progress, completionHandler: { result in
- switch result {
- case .success(let value):
- print("Task done for: \(value.source.url?.absoluteString ?? "")")
- completion?(.success(value.image))
- case .failure(let error):
- print("Job failed: \(error.localizedDescription)")
- completion?(.failure(error))
- }
- })
- }
-
- }
|