UIImageView+Ex.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // UIImageView+Ex.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2024/12/20.
  6. //
  7. import Kingfisher
  8. class TSCustomActivityIndicator: Indicator {
  9. let activityIndicator = UIActivityIndicatorView(style: .medium)
  10. init(color: UIColor) {
  11. activityIndicator.color = color
  12. }
  13. func startAnimatingView() {
  14. activityIndicator.startAnimating()
  15. }
  16. func stopAnimatingView() {
  17. activityIndicator.stopAnimating()
  18. }
  19. var view: IndicatorView {
  20. return activityIndicator
  21. }
  22. }
  23. public extension UIImageView {
  24. /// 创建并配置 UIImageView
  25. /// - Parameters:
  26. /// - imageName: 图片名称
  27. /// - contentMode: 内容模式,默认为 `.scaleAspectFit`
  28. /// - backgroundColor: 背景颜色,默认为透明
  29. /// - Returns: 配置完成的 UIImageView 实例
  30. static public func createImageView(
  31. image:UIImage? = nil,
  32. imageName: String? = nil,
  33. contentMode: UIView.ContentMode = .scaleAspectFit,
  34. backgroundColor: UIColor = .clear,
  35. corner: CGFloat = 0.0,
  36. autoMirrored: Bool = true
  37. ) -> UIImageView {
  38. let imageView = UIImageView()
  39. if let image = image{
  40. imageView.image = image
  41. }
  42. if let imageName = imageName ,imageName.count > 0 {
  43. if autoMirrored {
  44. imageView.image = UIImage(named: imageName)
  45. }else{
  46. imageView.image = UIImage(named: imageName)
  47. }
  48. }
  49. if let image = imageView.image,autoMirrored{
  50. imageView.image = image.mirrored
  51. }
  52. imageView.contentMode = contentMode
  53. imageView.backgroundColor = backgroundColor
  54. imageView.cornerRadius = corner
  55. return imageView
  56. }
  57. /// 异步创建并加载图片的 UIImageView
  58. /// - Parameters:
  59. /// - imageName: 本地占位图片名称
  60. /// - urlString: 图片的 URL 字符串
  61. /// - contentMode: 内容模式,默认为 `.scaleAspectFit`
  62. /// - backgroundColor: 背景颜色,默认为透明
  63. /// - showLoading: 是否显示加载动画,默认为 `true`
  64. /// - completion: 图片加载成功后的回调
  65. static public func createAsyncImageView(urlString: String?,
  66. placeholder:UIImage?,
  67. contentMode: UIView.ContentMode = .scaleAspectFit,
  68. adaptiveMode:Bool = false,
  69. backgroundColor: UIColor = .clear,
  70. showLoading: Bool = false,
  71. progressBlock: ((Float)->Void)? = nil,
  72. completion: ((UIImage?) -> Void)? = nil) -> UIImageView {
  73. let imageView = UIImageView()
  74. imageView.setAsyncImage(urlString: urlString,
  75. placeholder:placeholder,
  76. contentMode:contentMode,
  77. adaptiveMode:adaptiveMode,
  78. backgroundColor:backgroundColor,
  79. showLoading:showLoading,
  80. progressBlock:progressBlock,
  81. completion:completion)
  82. return imageView
  83. }
  84. public func setAsyncImage(urlString: String?,
  85. placeholder: UIImage? = nil,
  86. contentMode: UIView.ContentMode? = nil,
  87. adaptiveMode:Bool = false,
  88. backgroundColor: UIColor? = nil,
  89. showLoading: Bool = false,
  90. progressBlock: ((Float)->Void)? = nil,
  91. completion: ((UIImage?) -> Void)? = nil){
  92. let imageView = self
  93. if let contentMode = contentMode {
  94. imageView.contentMode = contentMode
  95. }
  96. if let backgroundColor = backgroundColor {
  97. imageView.backgroundColor = backgroundColor
  98. }
  99. imageView.image = placeholder
  100. guard let urlString = urlString else {
  101. completion?(nil)
  102. return
  103. }
  104. if urlString.count == 0 {
  105. completion?(nil)
  106. return
  107. }
  108. if urlString.contains("http") {
  109. guard let url = URL(string: urlString) else {
  110. completion?(nil)
  111. return
  112. }
  113. kf.indicatorType = showLoading ? .custom(indicator: TSCustomActivityIndicator(color: .white)) : .none
  114. imageView.kf.setImage(with: url,
  115. placeholder: placeholder,
  116. options: nil,
  117. progressBlock: { receivedSize, totalSize in
  118. let progress = receivedSize/totalSize
  119. progressBlock?(Float(progress))
  120. }){ result in
  121. if let image = try? result.get().image {
  122. kMainShort {
  123. completion?(image)
  124. }
  125. }else{
  126. completion?(nil)
  127. }
  128. }
  129. }else if urlString.contains("/") {
  130. imageView.image = UIImage(contentsOfFile: urlString)
  131. completion?(imageView.image)
  132. }else {
  133. if let image = UIImage(named: urlString) {
  134. imageView.image = image
  135. completion?(image)
  136. }
  137. }
  138. }
  139. }
  140. public extension UIImageView {
  141. static public func createRightArrow() -> UIImageView {
  142. let imageView = UIImageView()
  143. imageView.image = UIImage(named: "right_arrow")?.mirrored
  144. return imageView
  145. }
  146. /// 根据图片比例自动选择最佳缩放模式
  147. public func adaptiveScale() {
  148. // guard let image = image else { return }
  149. //
  150. // let viewSize = bounds.size
  151. // let imageRatio = image.size.width / image.size.height
  152. // let viewRatio = viewSize.width / viewSize.height
  153. //
  154. // let contentNewMode:UIView.ContentMode = imageRatio > viewRatio ? .scaleAspectFill : .scaleAspectFit
  155. // self.contentMode = contentNewMode
  156. //
  157. // dePrint("UIImageView.adaptiveScale contentMode =\(contentNewMode)")
  158. }
  159. }
  160. public extension UIImageView {
  161. public func setImage(_ image: UIImage?, duration: CFTimeInterval = 0.2, animated: Bool = true) {
  162. if let image = image {
  163. if animated {
  164. UIView.transition(
  165. with: self,
  166. duration: duration,
  167. options: [.transitionCrossDissolve, .curveEaseInOut, .allowUserInteraction]
  168. ) {
  169. self.image = image
  170. }
  171. }else {
  172. self.image = image
  173. }
  174. }
  175. }
  176. }