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,但我不知道如何修改呢?我會怎麼做?
我得到它的工作,但感謝撰寫解決方案! – Jade