我正在使用AlamofireImage从我的CDN下载图像,但我想将自定义过滤器(灰度)应用于下载的某些图像。目前我使用scrollView显示所有可用图像,如果图像具有特定属性,请应用过滤器。
我是AlamofireImage的新手,所以我不知道如何应用自定义过滤器。
我申请了
reloadData
到filter参数,所以我知道实际的过滤器进程正在运行,但有人可以帮助创建自定义过滤器以将灰度应用于下载的图像吗?
答案 0 :(得分:4)
方法#1
使用Alamofireimage" imageWithAppliedCoreImageFilter" UIImage扩展。但由于某些原因,他们没有提供简单的协议访问此过滤器选项,所以让我们创建一个......
public struct CoreImageFilter: ImageFilter {
let filterName: String
let parameters: [String: AnyObject]
public init(filterName : String, parameters : [String : AnyObject]?) {
self.filterName = filterName
self.parameters = parameters ?? [:]
}
public var filter: UIImage -> UIImage {
return { image in
return image.af_imageWithAppliedCoreImageFilter(self.filterName, parameters: self.parameters) ?? image
}
}
Alamofireimage用户应该熟悉用法:
let imageView = UIImageView(frame: frame)
let URL = NSURL(string: "https://httpbin.org/image/png")!
let placeholderImage = UIImage(named: "placeholder")!
let color = CIColor(color: UIColor.grayColor()) // or whatever color desired
let intensity = 1.0 // 1.0 is the default, but setting it manually here to allow adjustment
let filter = CoreImageFilter(filterName: "CIColorMonochrome", parameters: ["inputColor": color, "inputIntensity" : intensity])
imageView.af_setImageWithURL(
URL,
placeholderImage: placeholderImage,
filter: filter
)
如果你对CIColorMonochrome滤镜不满意(我不是),这里有一些其他的选择......
let filter = CoreImageFilter(filterName: "CIPhotoEffectTonal", parameters: nil)
或
let filter = CoreImageFilter(filterName: "CIPhotoEffectNoir", parameters: nil)
这是Apple开发网站上complete list of available filters的链接。
方法#2
查找自定义过滤条件,例如What is the best Core Image filter to produce black and white effects?的评分最高的答案。
然后创建一个扩展名。对于以下代码正文,请Shmidt和DerGote。
extension UIImage {
public func ff_imageFilteredToGrayScale() -> UIImage? {
let context = CIContext(options: nil)
let ciImage = CoreImage.CIImage(image: self)!
// Set image color to b/w
let bwFilter = CIFilter(name: "CIColorControls")!
bwFilter.setValuesForKeysWithDictionary([kCIInputImageKey:ciImage, kCIInputBrightnessKey:NSNumber(float: 0.0), kCIInputContrastKey:NSNumber(float: 1.1), kCIInputSaturationKey:NSNumber(float: 0.0)])
let bwFilterOutput = (bwFilter.outputImage)!
// Adjust exposure
let exposureFilter = CIFilter(name: "CIExposureAdjust")!
exposureFilter.setValuesForKeysWithDictionary([kCIInputImageKey:bwFilterOutput, kCIInputEVKey:NSNumber(float: 0.7)])
let exposureFilterOutput = (exposureFilter.outputImage)!
// Create UIImage from context
let bwCGIImage = context.createCGImage(exposureFilterOutput, fromRect: ciImage.extent)
let resultImage = UIImage(CGImage: bwCGIImage, scale: 1.0, orientation: self.imageOrientation)
return resultImage
}
}
和结构......
public struct GrayScaleFilter: ImageFilter {
public init() {
}
public var filter: UIImage -> UIImage {
return { image in
return image.ff_imageFilteredToGrayScale() ?? image
}
}
}
最后,用法......
let imageView = UIImageView(frame: frame)
let URL = NSURL(string: "https://httpbin.org/image/png")!
let placeholderImage = UIImage(named: "placeholder")!
let filter = GrayScaleFilter()
imageView.af_setImageWithURL(
URL,
placeholderImage: placeholderImage,
filter: filter
)