2016-04-11 263 views
2

我正在使用AlamofireImage從我的CDN下載圖像,但是我想將自定義過濾器(灰度)應用於某些下載的圖像。目前我正在使用scrollView來顯示所有可用圖像,如果圖像具有特定屬性,請應用濾鏡。如何應用自定義過濾器

我是AlamofireImage的新手,所以我不知道如何應用自定義過濾器。

我已經申請

let imageFilter = BlurFilter(blurRadius: 10) 

的濾波器參數,所以我知道實際的濾波處理工作,但有人可以請創建自定義過濾器,以灰度適用於下載圖像幫助嗎?

回答

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?

然後創建一個擴展。以下代碼正文爲信用ShmidtDerGote

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 
)