微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift2 – Swift – GLKit查看CIFilter图像

我正在尝试使用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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐