如何应用自定义过滤器 [英] How do I apply a Custom Filter

查看:134
本文介绍了如何应用自定义过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AlamofireImage从CDN下载图像,但是我想对某些已下载的图像应用自定义滤镜(灰度)。目前,我正在使用scrollView显示所有可用图像,并且如果图像具有特定属性,则应用滤镜。

I am using AlamofireImage to download an image from my CDN however I would like to apply a custom filter (Grey Scale) to SOME of the images that are downloaded. Currently I am using a scrollView to show all available images and if an image has a particular attribute, apply the filter.

我是AlamofireImage的新手,所以我不知道如何应用自定义过滤器。

I am new to AlamofireImage so I don't know how to apply a custom filter.

我已应用

let imageFilter = BlurFilter(blurRadius: 10)

过滤器参数,因此我知道实际的过滤器工作正常但是有人可以帮助创建自定义滤镜以将灰度应用于下载的图像吗?

to the filter parameter so I know the actual filter process is working but can someone please help with creating a custom filter to apply grey scale to downloaded image?

推荐答案

方法#1

使用Alamofireimage的 imageWithAppliedCoreImageFilter UIImage扩展。但是由于某些原因,它们不提供对此过滤器选项的简便协议访问,因此让我们创建一个...

Use Alamofireimage's "imageWithAppliedCoreImageFilter" UIImage extension. But for some reason they don't provide easy protocol access to this filter option, so let's create one...

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用户应熟悉用法:

Usage should be familiar to the Alamofireimage user:

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滤镜(我不是),这里有一些其他选项...

If you're not happy with the CIColorMonochrome filter (I wasn't), here are some other options...

let filter = CoreImageFilter(filterName: "CIPhotoEffectTonal", parameters: nil)

let filter = CoreImageFilter(filterName: "CIPhotoEffectNoir", parameters: nil)

这里是 Apple开发者网站上可用过滤器的完整列表

方法#2

查找自定义过滤器,例如评分最高的过滤器回答什么是最好的

Find a custom filter, such as the top rated answer to What is the best Core Image filter to produce black and white effects?.

然后创建扩展名。归功于 Shmidt DerGote

Then create a extension. Credit Shmidt and DerGote for the body of the code below.

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
)

这篇关于如何应用自定义过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆