2017-06-15 55 views
0

我想創建一個自定義的CIFilter。到目前爲止,這個過濾器只是使圖像變亮,並不是我真正想要的,但我正在做這件事,看看我能否實現它。儘管過濾器使用inputBlurRadius的設置值,但我不知道如何更改它,以便用戶可以更改該值。這是自定義過濾器。如何修改自定義CIFilter的inputKeys?

import Foundation 
import UIKit 

public class CustomFilter: CIFilter { 
    var inputImage: CIImage? 
    private var kernel: CIColorKernel! 
    var inputBrightness: CGFloat = 0.4 

override public var outputImage: CIImage! 
{ 
    guard let inputImage = inputImage 
     else 
    { 
     return nil 
    } 

    let extent = inputImage.extent 

    let blur = createCustomKernel4().apply(
     withExtent: inputImage.extent, 
     roiCallback: 
     { 
      (index, rect) in 
      return rect 
    }, 
     arguments: [inputImage, inputBrightness as AnyObject]) 

    return blur!.cropping(to: extent) 
} 

func createCustomKernel4() -> CIColorKernel { 
    let kernelString = 
     "kernel vec4 brightenEffect (sampler src , float k) { \n " + 
      " vec4 currentSource = sample (src, samplerCoord (src));" + 
      "currentSource.rgb = currentSource.rgb + k * currentSource.a;" + 
      "return currentSource; \n " + 
    "}" 
    return CIColorKernel(string: kernelString)! 
    //brigtens an image 
} 
} 


class CustomFiltersVendor: NSObject, CIFilterConstructor 
{ 
    static func registerFilters() 
    { 
    CIFilter.registerName(
     "CustomFilter", 
     constructor: CustomFiltersVendor(), 
     classAttributes: [ 
      kCIAttributeFilterCategories: ["CustomFilters"] 
     ]) 
} 

func filter(withName name: String) -> CIFilter? 
{ 
    switch name 
    { 
    case "CustomFilter": 
     return CustomFilter() 

    default: 
     return nil 
    } 
} 
} 

在,我想在使用它的文件,我有

let filter3 = CustomFilter() 
filter3.setValue(coreImage, forKey: kCIInputImageKey) 
filter3.inputKeys 
let outputImage = filter3.outputImage! 

我確實看到inputBrightness在inputKeys,但我不知道如何修改呢?我會怎麼做?

回答

0

最新回答,但我只是遇到了這個問題。如果你還沒有,子類CIFilter

你的內核代碼意味着你想傳遞一個值爲float k。假設你想稱之爲inputBrightnessFactor。下面是你在子類中需要什麼:注意

class CustomFilter:CIFilter { 

    let kernel = CIColorKernel(source: 
     "kernel vec4 brightenEffect (sampler src , float k) { \n " + 
      "vec4 currentSource = sample (src, samplerCoord (src));" + 
      "currentSource.rgb = currentSource.rgb + k * currentSource.a;" + 
      "return currentSource; \n " + 
     "}") 
    var inputImage:CIImage! 
    var inputBrightnessFactor:Float = 1 

    override var attributes: [String : Any] { 
     return [ 
      kCIAttributeFilterDisplayName: "My Custom Filter", 

      "inputImage": [kCIAttributeIdentity: 0, 
          kCIAttributeClass: "CIImage", 
          kCIAttributeDisplayName: "Image", 
          kCIAttributeType: kCIAttributeTypeImage], 

      "inputBrightnessFactor": [kCIAttributeIdentity: 0, 
             kCIAttributeClass: "NSNumber", 
             kCIAttributeDisplayName: "Brightness Factor", 
             kCIAttributeDefault: 0, 
             kCIAttributeMin: 0, 
             kCIAttributeSliderMin: 0, 
             kCIAttributeSliderMax: 1, 
             kCIAttributeType: kCIAttributeTypeScalar] 
     ] 
    } 

    override init() { 
     super.init() 
    } 

    override func setValue(_ value: Any?, forKey key: String) { 
     switch key { 
      case "inputImage": 
       inputImage = value as! CIImage 
      case "inputBrightnessFactor": 
       inputBrightnessFactor = value as! Float 
      default: 
       break 
     } 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override var outputImage: CIImage { 
     return kernel!.apply(extent: inputImage.extent, arguments: [inputImage as Any, inputBrightnessFactor as Any])! 
    } 
} 

兩個關鍵的東西:

  • 您覆蓋attributes爲過濾器和屬性寄存器顯示值,並給出了屬性的默認值。
  • 您覆蓋setValue(forKey:)以允許您像使用其他任何過濾器一樣使用它。

使用(請確保您運行CustomFilters.registerFilters()在前):

let myFactor:Float = 0.4 
let filter = CIFilter(name: "CustomFilter") 
filter.setValue(inputImage, forKey: kCIInputImageKey) 
filter.setValue(myFactor, forKey: "inputBrightnessFactor") 
let myOutputImage = filter?.outputImage 

注意,你現在把你的內核像任何其他CIFilter

希望你已經有了工作,但如果沒有,應該這樣做。

+0

我得到它的工作,但感謝撰寫解決方案! – Jade