ios - 如何有效地应用 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)。然后将彩色滤光片送入 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.
}
推荐阅读
- c# - DecryptByPassPhrase 的等效 C# 代码,反之亦然
- linux - 如何使用 sed 将 virsh xml 数据转换为 PCI BDF 格式
- unity3d - 导入到 unity 的 Blender 文件在每个轴上具有不同的缩放比例
- visual-studio-code - 在 vscode 中设置隐藏开发工具弹出窗口
- mongodb - 带有 Like 的 MongoDB 查询
- php - Laravel 中的 required_if 验证
- javascript - 使用 javascript 创建 xls 文件,下载不支持的格式
- php - 变量存在且为真时的刀片条件渲染
- python - django.db.utils.ProgrammingError:关系“ogs_features_product”的列“价格”不存在
- c# - c# camt54 (iso 20022) 中的日期时间反序列化问题