swift 如何在 kingFisher 库中使用 AspectScaledToFitAndCenterSizeFilter
How to use AspectScaledToFitAndCenterSizeFilter in kingFisher library in swift
在我的项目中,我在 swift 中使用 AlamofireImage。现在我们用 KingFisher 库替换了 AlamofireImage。我创建了一个使用下面的结构来适应过滤器
struct AspectScaledToFitAndCenterSizeFilter: ImageFilter, Sizable {
/// The size of the filter.
let size: CGSize
/// Initializes the `AspectScaledToFitSizeFilter` instance with the given size.
///
/// - parameter size: The size.
///
/// - returns: The new `AspectScaledToFitSizeFilter` instance.
init(size: CGSize) {
self.size = size
}
/// The filter closure used to create the modified representation of the given image.
var filter: (UIImage) -> UIImage {
{ image in
image.imageAspectScaledAndCenter(toFit: self.size)
}
}
}
当我们使用 AlmofireImage 时使用下面的代码设置图像 url
imageView.af.setImage(withURL: imageURL.mediaURL(), placeholderImage: #imageLiteral(resourceName: "icMissingEntreeGrid"), filter: AspectScaledToFitAndCenterSizeFilter(size: imageSize))
现在我将代码替换为
imageView.kf.setImage(with: imageURL.mediaURL(), placeholder: imageLiteral(resourceName: "icMissingEntreeGrid"))
但是如何使用 KingFisher 库添加“AspectScaledToFitAndCenterSizeFilter(size: imageSize)”。谁能帮我解决这个问题。提前致谢。
要创建 Kingfisher 图像处理器,您需要实施 ImageProcessor
协议:
class AspectScaledToFitAndCenterSizeFilter: ImageProcessor {
/// Identifier of the processor.
/// - Note: See documentation of `ImageProcessor` protocol for more.
let identifier: String
/// The size of the filter.
let size: CGSize
/// Initializes the `AspectScaledToFitSizeFilter` instance with the given size.
///
/// - parameter size: The size.
///
/// - returns: The new `AspectScaledToFitSizeFilter` instance.
init(size: CGSize) {
self.size = size
identifier = "com.package.AspectScaledToFitAndCenterSizeFilter(\(size))"
}
func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
switch item {
case .image(let image):
return image.imageAspectScaledAndCenter(toFit: self.size)
case .data:
return (DefaultImageProcessor.default |> self).process(item: item, options: options)
}
}
}
用法:
imageView.kf.setImage(
with: imageURL.mediaURL(),
placeholder: #imageLiteral(resourceName: "icMissingEntreeGrid"),
options: [
.processor(AspectScaledToFitAndCenterSizeFilter(size: .zero))
]
)
查看 documentation 中的更多处理器使用情况。
在我的项目中,我在 swift 中使用 AlamofireImage。现在我们用 KingFisher 库替换了 AlamofireImage。我创建了一个使用下面的结构来适应过滤器
struct AspectScaledToFitAndCenterSizeFilter: ImageFilter, Sizable {
/// The size of the filter.
let size: CGSize
/// Initializes the `AspectScaledToFitSizeFilter` instance with the given size.
///
/// - parameter size: The size.
///
/// - returns: The new `AspectScaledToFitSizeFilter` instance.
init(size: CGSize) {
self.size = size
}
/// The filter closure used to create the modified representation of the given image.
var filter: (UIImage) -> UIImage {
{ image in
image.imageAspectScaledAndCenter(toFit: self.size)
}
}
}
当我们使用 AlmofireImage 时使用下面的代码设置图像 url
imageView.af.setImage(withURL: imageURL.mediaURL(), placeholderImage: #imageLiteral(resourceName: "icMissingEntreeGrid"), filter: AspectScaledToFitAndCenterSizeFilter(size: imageSize))
现在我将代码替换为
imageView.kf.setImage(with: imageURL.mediaURL(), placeholder: imageLiteral(resourceName: "icMissingEntreeGrid"))
但是如何使用 KingFisher 库添加“AspectScaledToFitAndCenterSizeFilter(size: imageSize)”。谁能帮我解决这个问题。提前致谢。
要创建 Kingfisher 图像处理器,您需要实施 ImageProcessor
协议:
class AspectScaledToFitAndCenterSizeFilter: ImageProcessor {
/// Identifier of the processor.
/// - Note: See documentation of `ImageProcessor` protocol for more.
let identifier: String
/// The size of the filter.
let size: CGSize
/// Initializes the `AspectScaledToFitSizeFilter` instance with the given size.
///
/// - parameter size: The size.
///
/// - returns: The new `AspectScaledToFitSizeFilter` instance.
init(size: CGSize) {
self.size = size
identifier = "com.package.AspectScaledToFitAndCenterSizeFilter(\(size))"
}
func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
switch item {
case .image(let image):
return image.imageAspectScaledAndCenter(toFit: self.size)
case .data:
return (DefaultImageProcessor.default |> self).process(item: item, options: options)
}
}
}
用法:
imageView.kf.setImage(
with: imageURL.mediaURL(),
placeholder: #imageLiteral(resourceName: "icMissingEntreeGrid"),
options: [
.processor(AspectScaledToFitAndCenterSizeFilter(size: .zero))
]
)
查看 documentation 中的更多处理器使用情况。