// UIImageViewExtensions.swift - Copyright 2023 SwifterSwift
|
|
#if canImport(UIKit) && !os(watchOS)
|
import UIKit
|
|
// MARK: - Methods
|
|
public extension UIImageView {
|
/// SwifterSwift: Set image from a URL.
|
///
|
/// - Parameters:
|
/// - url: URL of image.
|
/// - contentMode: imageView content mode (default is .scaleAspectFit).
|
/// - placeHolder: optional placeholder image
|
/// - completionHandler: optional completion handler to run when download finishes (default is nil).
|
func download(
|
from url: URL,
|
contentMode: UIView.ContentMode = .scaleAspectFit,
|
placeholder: UIImage? = nil,
|
completionHandler: ((UIImage?) -> Void)? = nil) {
|
image = placeholder
|
self.contentMode = contentMode
|
URLSession.shared.dataTask(with: url) { data, response, _ in
|
guard
|
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
|
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
|
let data = data,
|
let image = UIImage(data: data) else {
|
completionHandler?(nil)
|
return
|
}
|
DispatchQueue.main.async { [unowned self] in
|
self.image = image
|
completionHandler?(image)
|
}
|
}.resume()
|
}
|
|
/// SwifterSwift: Make image view blurry.
|
///
|
/// - Parameter style: UIBlurEffectStyle (default is .light).
|
func blur(withStyle style: UIBlurEffect.Style = .light) {
|
let blurEffect = UIBlurEffect(style: style)
|
let blurEffectView = UIVisualEffectView(effect: blurEffect)
|
blurEffectView.frame = bounds
|
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // for supporting device rotation
|
addSubview(blurEffectView)
|
clipsToBounds = true
|
}
|
|
/// SwifterSwift: Blurred version of an image view.
|
///
|
/// - Parameter style: UIBlurEffectStyle (default is .light).
|
/// - Returns: blurred version of self.
|
func blurred(withStyle style: UIBlurEffect.Style = .light) -> UIImageView {
|
let imgView = self
|
imgView.blur(withStyle: style)
|
return imgView
|
}
|
}
|
|
#endif
|