Как эффективно применять фильтры GPUImage? - PullRequest
0 голосов
/ 03 июля 2018

В моем проекте я применяю цвета к изображению, затем через секунду я применяю к нему фильтр Kuwahara для эффекта акварели, но дело в том, что для применения фильтра требуется время, и если я слишком сильно меняю цвета, приложение в конечном итоге падает из-за проблем с памятью. Может ли кто-нибудь помочь мне, как использовать фильтр наилучшим образом. Спасибо

CODE

@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
        }


    })

}

1 Ответ

0 голосов
/ 04 июля 2018

Вот как я бы просто настроил фильтры, используя GPUImage2 в быстром темпе, основываясь на том, что, я думаю, вы ищете. Изображение в цветной фильтр, затем в источник 1 растворяемой смеси (смесь 0.0). Затем отправьте цветной фильтр в фильтр Kuwahara, а затем в источник 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.
}
...