当前位置 : 主页 > 手机开发 > 其它 >

swift2 – Swift – GLKit查看CIFilter图像

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在尝试使用GLIKit View来修改图像.到目前为止,我所有的CIFilters工作都很好,除了CILineOverlay它呈现黑色视图.如果我使用任何其他效果,它运作良好. 为什么CILineOverlay没有显示? class Ima
我正在尝试使用GLIKit View来修改图像.到目前为止,我所有的CIFilters工作都很好,除了CILineOverlay它呈现黑色视图.如果我使用任何其他效果,它运作良好.

为什么CILineOverlay没有显示?

class ImageView: GLKView {
    let clampFilter = CIFilter(name: "CIAffineClamp")!
    let blurFilter = CIFilter(name: "CILineOverlay")!
    let ciContext:CIContext

    override init(frame: CGRect) {
        let glContext = EAGLContext(API: .OpenGLES2)
        ciContext = CIContext(
            EAGLContext: glContext,
            options: [
                kCIContextWorkingColorSpace: NSNull()
            ]
        )
        super.init(frame: frame, context: glContext)
        enableSetNeedsDisplay = true
    }

    required init(coder aDecoder: NSCoder) {
        let glContext = EAGLContext(API: .OpenGLES2)
        ciContext = CIContext(
            EAGLContext: glContext,
            options: [
                kCIContextWorkingColorSpace: NSNull()
            ]
        )
        super.init(coder: aDecoder)!
        context = glContext
        enableSetNeedsDisplay = true
    }

    @IBInspectable var inputImage: UIImage? {
        didSet {
            inputCIImage = inputImage.map { CIImage(image: $0)! }
        }
    }

    @IBInspectable var blurRadius: Float = 0 {
        didSet {
            //blurFilter.setValue(blurRadius, forKey: "inputIntensity")
            setNeedsDisplay()
        }
    }

    var inputCIImage: CIImage? {
        didSet { setNeedsDisplay() }
    }

    override func drawRect(rect: CGRect) {
        if let inputCIImage = inputCIImage {
            clampFilter.setValue(inputCIImage, forKey: kCIInputImageKey)
            blurFilter.setValue(clampFilter.outputImage!, forKey: kCIInputImageKey)
            let rect = CGRect(x: 0, y: 0, width: drawableWidth, height: drawableHeight)
            ciContext.drawImage(blurFilter.outputImage!, inRect: rect, fromRect: inputCIImage.extent)
        }
    }
}
Apple文档指出“未概述的图像部分是透明的.” – 这意味着您在黑色背景上绘制黑色线条.您可以简单地将滤镜的输出合成为白色背景,以使线条显示:

let background = CIImage(color: CIColor(color: UIColor.whiteColor()))
        .imageByCroppingToRect(inputCIImage.extent)

    let finalImage = filter.outputImage!
        .imageByCompositingOverImage(background)
网友评论