ios - 如何有效地应用 GPUImage 过滤器?

标签 ios swift gpuimage

在我的项目中,我将颜色应用于图像,然后在一秒钟后我将 Kuwahara 过滤器应用于它,以获得水彩效果,但问题是应用过滤器需要时间,如果我改变颜色太多应用程序最终因内存问题而崩溃。任何人都可以帮助我如何以最佳方式使用过滤器。谢谢

代码

@objc func fillColorButtonTapped(_ sender : UIButton){

    self.processingModel.isImageMixedColor = false

    if let popoverController = self.mkColorPicker.popoverPresentationController{
        popoverController.delegate = self.mkColorPicker
        popoverController.permittedArrowDirections = .any
        popoverController.sourceView = sender
        popoverController.sourceRect = sender.bounds
    }

    self.present(self.mkColorPicker, animated: true, completion: nil)

    self.mkColorPicker.selectedColor = { [weak self] color in

        guard let strongSelf = self else {
            return
        }

        let image = ChangeColor.image(byReplacingColor: strongSelf.processingModel.pencileDefaultImage, withSourceColor: .black, withMinTolerance: 0.4, withMaxTolerance: 0.5, with: color)
        strongSelf.processingModel.croppedImageToWorkOn =  image
        UIView.transition(with: strongSelf.handAndFootImageView,
                          duration: 0.2,
                          options: .transitionCrossDissolve,
                          animations: {strongSelf.handAndFootImageView.image = strongSelf.processingModel.croppedImageToWorkOn},
                          completion: nil)

        strongSelf.addWaterColorEffect()

    }
}

func addWaterColorEffect(withRadius : Int = 5){


    CommonClass.delayWithSeconds(0.5, completion: {

        let filter = KuwaharaFilter()
        filter.radius = withRadius
        let imageToFilter = self.containerView.toImage()
        DispatchQueue.main.async {

            let imageToShow =  imageToFilter.filterWithOperation(filter)

            UIView.transition(with: self.handAndFootImageView,
                              duration: 0.6,
                              options: .transitionCrossDissolve,
                              animations: {self.handAndFootImageView.image = imageToShow },
                              completion: nil)

            self.processingModel.croppedImageToWorkOn =  imageToShow
        }


    })

}

最佳答案

这就是我如何根据我认为您所追求的快速使用 GPUImage2 设置过滤器。图像进入滤色器,然后进入溶解混合的源 1(混合 0.0)。然后将彩色滤光片送入桑原滤光片,然后送入溶解混合物的来源 2。从那里你可以在两者之间转换并根据需要改变半径。

func setupFilters() {
    image --> colorFilter --> dissolveBlend
    colorFilter --> kuwaharaFilter --> dissolveBlend --> renderView
    dissolveBlend.mix = 0.0
}

func addWaterColorEffect(withRadius : Int = 5){
    kuwaharaFilter.radius = withRadius
    dissolveBlend.mix = 1.0 
    // This will not give you a transition but you can use a while loop or timer 
    // to change the mix over the course of whatever length of time you are seeking.
}

关于ios - 如何有效地应用 GPUImage 过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51151329/

相关文章:

ios - Swift - 如何在自定义 AlertController 中点击按钮时呈现 ViewController

ios - UIWebView 在 Safari 中打开链接

ios - 在自定义 UITableViewCell 中调整 UITextView 的大小

swift - 正确的缓冲区大小

ios - 确保 TextField 实际上包含文本

ios - UISeachBar 和委托(delegate)方法

swift - 我想回到 TabBarController 中的上一个 viewController

c++ - 在 OpenCV 上使用 GPU 时如何确定线程数?

ios - 如何使用自动闪光模式检测闪光灯开/关?

ios - 基本 GPUImage iOS7 应用程序视频卡住/不工作