在我的项目中,我为图像添加颜色,然后过一秒钟,我为其应用了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快速设置过滤器的方式。图像进入彩色滤光片,然后进入溶解混合物(混合物0.0)的源1。然后将颜色滤镜发送到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.
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句