123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- //
- // UIImageView+Ex.swift
- // TSLiveWallpaper
- //
- // Created by 100Years on 2024/12/20.
- //
- import Kingfisher
- class TSCustomActivityIndicator: Indicator {
- let activityIndicator = UIActivityIndicatorView(style: .medium)
-
- init(color: UIColor) {
- activityIndicator.color = color
- }
-
- func startAnimatingView() {
- activityIndicator.startAnimating()
- }
-
- func stopAnimatingView() {
- activityIndicator.stopAnimating()
- }
-
- var view: IndicatorView {
- return activityIndicator
- }
- }
- public extension UIImageView {
- /// 创建并配置 UIImageView
- /// - Parameters:
- /// - imageName: 图片名称
- /// - contentMode: 内容模式,默认为 `.scaleAspectFit`
- /// - backgroundColor: 背景颜色,默认为透明
- /// - Returns: 配置完成的 UIImageView 实例
- static public func createImageView(
- image:UIImage? = nil,
- imageName: String? = nil,
- contentMode: UIView.ContentMode = .scaleAspectFit,
- backgroundColor: UIColor = .clear,
- corner: CGFloat = 0.0) -> UIImageView {
- let imageView = UIImageView()
-
- if let image = image{
- imageView.image = image
- }
- if let imageName = imageName ,imageName.count > 0 {
- imageView.image = UIImage(named: imageName)
- }
-
- imageView.contentMode = contentMode
- imageView.backgroundColor = backgroundColor
- imageView.cornerRadius = corner
- return imageView
- }
-
- /// 异步创建并加载图片的 UIImageView
- /// - Parameters:
- /// - imageName: 本地占位图片名称
- /// - urlString: 图片的 URL 字符串
- /// - contentMode: 内容模式,默认为 `.scaleAspectFit`
- /// - backgroundColor: 背景颜色,默认为透明
- /// - showLoading: 是否显示加载动画,默认为 `true`
- /// - completion: 图片加载成功后的回调
- static public func createAsyncImageView(urlString: String?,
- placeholder:UIImage?,
- contentMode: UIView.ContentMode = .scaleAspectFit,
- adaptiveMode:Bool = false,
- backgroundColor: UIColor = .clear,
- showLoading: Bool = false,
- progressBlock: ((Float)->Void)? = nil,
- completion: ((UIImage?) -> Void)? = nil) -> UIImageView {
- let imageView = UIImageView()
- imageView.setAsyncImage(urlString: urlString,
- placeholder:placeholder,
- contentMode:contentMode,
- adaptiveMode:adaptiveMode,
- backgroundColor:backgroundColor,
- showLoading:showLoading,
- progressBlock:progressBlock,
- completion:completion)
- return imageView
- }
-
-
- public func setAsyncImage(urlString: String?,
- placeholder: UIImage? = nil,
- contentMode: UIView.ContentMode = .scaleAspectFit,
- adaptiveMode:Bool = false,
- backgroundColor: UIColor = .clear,
- showLoading: Bool = false,
- progressBlock: ((Float)->Void)? = nil,
- completion: ((UIImage?) -> Void)? = nil){
- let imageView = self
- imageView.contentMode = contentMode
- imageView.backgroundColor = backgroundColor
- imageView.image = placeholder
-
- guard let urlString = urlString else {
- completion?(nil)
- return
- }
-
- if urlString.count == 0 {
- completion?(nil)
- return
- }
-
- if urlString.contains("http") {
- guard let url = URL(string: urlString) else {
- completion?(nil)
- return
- }
- kf.indicatorType = showLoading ? .custom(indicator: TSCustomActivityIndicator(color: .white)) : .none
- imageView.kf.setImage(with: url,
- placeholder: placeholder,
- options: nil,
- progressBlock: { receivedSize, totalSize in
- let progress = receivedSize/totalSize
- progressBlock?(Float(progress))
- }){ result in
-
- if let image = try? result.get().image {
- kDelayMainShort {
- completion?(image)
- }
- }else{
- completion?(nil)
- }
- }
-
- }else if urlString.contains("/") {
- imageView.image = UIImage(contentsOfFile: urlString.fillCachePath)
- completion?(imageView.image)
- }else {
- if let image = UIImage(named: urlString) {
- imageView.image = image
- completion?(image)
- }
- }
- }
- }
- public extension UIImageView {
-
- static public func createRightArrow() -> UIImageView {
- let imageView = UIImageView()
- imageView.image = UIImage(named: "white_right_arrow")
- return imageView
- }
-
- /// 根据图片比例自动选择最佳缩放模式
- public func adaptiveScale() {
- // guard let image = image else { return }
- //
- // let viewSize = bounds.size
- // let imageRatio = image.size.width / image.size.height
- // let viewRatio = viewSize.width / viewSize.height
- //
- // let contentNewMode:UIView.ContentMode = imageRatio > viewRatio ? .scaleAspectFill : .scaleAspectFit
- // self.contentMode = contentNewMode
- //
- // dePrint("UIImageView.adaptiveScale contentMode =\(contentNewMode)")
- }
- }
- public extension UIImageView {
- public func setImage(_ image: UIImage?, duration: CFTimeInterval = 0.2, animated: Bool = true) {
- if let image = image {
- if animated {
- UIView.transition(
- with: self,
- duration: duration,
- options: [.transitionCrossDissolve, .curveEaseInOut, .allowUserInteraction]
- ) {
- self.image = image
- }
- }else {
- self.image = image
- }
- }
- }
- }
- public extension UIImageView {
- static func retrieveImageInMemoryCache(urlString: String) -> UIImage? {
- return ImageCache.default.retrieveImageInMemoryCache(forKey: urlString)
- }
-
- static func downloadImageWithProgress(
- urlString: String,
- progressHandler: ((Float) -> Void)? = nil,
- completion: @escaping (UIImage?) -> Void
- ) {
- guard let url = URL(string: urlString) else {
- completion(nil)
- return
- }
-
- KingfisherManager.shared.retrieveImage(
- with: url,
- options: [],
- progressBlock: { receivedSize, totalSize in
- let progress = Float(receivedSize) / Float(totalSize)
- DispatchQueue.main.async {
- progressHandler?(progress) // 回传进度(主线程)
- }
- },
- completionHandler: { result in
- DispatchQueue.main.async {
- switch result {
- case .success(let value):
- completion(value.image)
- case .failure:
- completion(nil)
- }
- }
- }
- )
- }
-
-
- }
|