Visual_Novel_iOS/crush/Crush/Src/Utils/Extensions/UIImageViewExt.swift

113 lines
3.3 KiB
Swift
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// UIImageView+Ext.swift
// E-Wow
//
// Created by lym on 2021/1/8.
//
import Foundation
import Kingfisher
import UIKit
extension UIImageView
{
private static let defaultOptions: KingfisherOptionsInfo = [
.transition(.none),
.loadDiskFileSynchronously,
]
/**
s3
bgColor(csbn)
*/
func loadImage(_ url: String?,
placeholder: Placeholder? = nil,
bgColor: UIColor? = .c.csbn,
options: KingfisherOptionsInfo? = defaultOptions)
{
backgroundColor = bgColor
kf.setImage(with: URL(string: url ?? ""), placeholder: placeholder, options: options)
}
/// - Parameters:
/// - urlString: URL
/// - placeholder:
/// - progressBlock:
/// - completionBlock:
func loadImage(
_ urlString: String?,
placeholder: Placeholder? = nil,
bgColor: UIColor? = .hexInt32(0xFBDEFF).withAlphaComponent(0.08),
progressBlock: ((_ receivedSize: Int64, _ totalSize: Int64) -> Void)? = nil,
completionBlock: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
) {
guard let urlString = urlString, let url = URL(string: urlString) else {
completionBlock?(.failure(.requestError(reason: .emptyRequest)))
return
}
kf.setImage(
with: url,
placeholder: placeholder,
options: nil,
progressBlock: { receivedSize, totalSize in
//
progressBlock?(receivedSize, totalSize)
},
completionHandler: { result in
//
completionBlock?(result)
}
)
}
}
struct ImageDownloader {
/// 使 Kingfisher completion
// static func downloadImage(
// from url: URL?,
// completion: @escaping (UIImage?) -> Void
// ) {
// guard let url = url else {
// completion(nil)
// return
// }
//
// KingfisherManager.shared.retrieveImage(with: url) { result in
// switch result {
// case .success(let value):
// completion(value.image) //
// case .failure(let error):
// print(" : \(error)")
// completion(nil)
// }
// }
// }
/// 使 Kingfisher String
static func downloadImage(
from urlString: String?,
completion: @escaping (UIImage?) -> Void
) {
guard
let urlString = urlString,
let url = URL(string: urlString)
else {
completion(nil)
return
}
KingfisherManager.shared.retrieveImage(with: url) { result in
switch result {
case .success(let value):
completion(value.image) // UIImage
case .failure(let error):
print("❌ 图片下载失败: \(error)")
completion(nil)
}
}
}
}