Swift不能从UIImage中移除CIFIlter



在swift上工作了一段时间,在处理这个核心图像框架方面遇到了麻烦。我能够成功地在图像上创建一个CIFilter,但我不确定如何删除它。图像被放置在一个UIView上它类似于snapchat的摄像头屏幕然后有一个imageview它是UIView的子视图图像被预览在上面。BTW使用最新版本的Xcode和iOS。

下面是应用黑白滤镜时的代码:

@IBAction func BW_Flt_Bt_Tapped(sender: UIButton) {
    let beginImage = CIImage(image: imagePreview.image!)
    let ciContext = CIContext(options: nil)
    let filter = CIFilter(name: "CIPhotoEffectNoir")
    var imgOrientation = imagePreview.image!.imageOrientation
    var imgScale = imagePreview.image?.scale
    filter!.setDefaults()
    filter!.setValue(beginImage, forKey: kCIInputImageKey)
    let filteredImageData = filter!.valueForKey(kCIOutputImageKey) as! CIImage
    let filteredImageRef = ciContext.createCGImage(filteredImageData, fromRect: filteredImageData.extent)
    imagePreview.image = UIImage(CGImage: filteredImageRef, scale: 1.0, orientation: UIImageOrientation.Right)
    self.orgImgBt.hidden = false
    self.orgImgL.hidden = false
    BWFlt.hidden = true
    BWLbl.hidden = true


}

-imagePreview是UIView的子视图

接下来是我的代码如何改变图像返回

@IBAction func orgImgPressed(sender: UIButton) {
     self.imagePreview.image = self.selctedImage
    orgImgBt.hidden = true
    orgImgL.hidden = true
   `enter code here` BWFlt.hidden = false
    `enter code here`BWLbl.hidden = false

            print("button was pressed")
        }

如果有人有任何建议,我将非常感激,谢谢!

您可以通过将前一个值保存在变量中或如果您有多个过滤器则将该值保存在字典中来实现此目的。当你应用一个新的滤镜或改变现有滤镜的值,只需将该值替换为新值,并将所有值应用于原始CIImage

例如,

    var allFilters = [String: [String: Any]]()
    var currentImage: CIImage!
    let kCIColorControls = "CIColorControls"
    override func viewDidLoad() {
        super.viewDidLoad()
        currentImage = CIImage(image: self.originalImage!)
    }
    func applyAvailableFilters() {
            if self.allFilters.count > 0 {            
                DispatchQueue.global(qos: .background).async {
                    var outputImage: CIImage!
                    self.blurFilter = CIFilter.init(name: "CIGaussianBlur")
                    self.brightnessFilter = CIFilter.init(name: "CIColorControls")
                    self.cropFilter = CIFilter.init(name: "CICrop")
                    if let blurProperty = self.allFilters[self.kCIGaussianBlur]?.first {
                        self.blurFilter.setValue(blurProperty.value, forKey: blurProperty.key)
                                                self.blurFilter.setValue(self.currentImage, forKey: kCIInputImageKey)
                        outputImage = self.blurFilter.outputImage
                    }
                    if let brightnessProperty = self.allFilters[self.kCIColorControls]?.first {
                        self.brightnessFilter.setValue(brightnessProperty.value, forKey: brightnessProperty.key)
                        if let outputImage = self.blurFilter.outputImage {
                            self.brightnessFilter.setValue(outputImage, forKey: kCIInputImageKey)
                        } else {
                            self.brightnessFilter.setValue(self.currentImage, forKey: kCIInputImageKey)
                        }
                        outputImage = self.brightnessFilter.outputImage
                    }
                    if let cropProperty = self.allFilters[self.kCICrop]?.first {
                        self.cropFilter.setValue(cropProperty.value, forKey: cropProperty.key)
                        if let outputImage = self.brightnessFilter.outputImage {
                            self.cropFilter.setValue(outputImage, forKey: kCIInputImageKey)
                        } else if let outputImage = self.blurFilter.outputImage {
                            self.cropFilter.setValue(outputImage, forKey: kCIInputImageKey)
                        } else {
                            self.cropFilter.setValue(self.currentImage, forKey: kCIInputImageKey)
                        }
                        outputImage = self.cropFilter.outputImage
                    }
                    if let image = outputImage {
                        let openGLContext = EAGLContext(api: .openGLES2)
                        let context = CIContext(eaglContext: openGLContext!)
                        if let cgimg = context.createCGImage(image, from: image.extent) {
                            let processedImage = UIImage(cgImage: cgimg)
                            DispatchQueue.main.async {
                                self.imageView.image = processedImage
                            }
                        }
                    } else {
                        DispatchQueue.main.async {
                            self.imageView.image = self.selectedImage
                        }
                    }
                }
            } else {
                DispatchQueue.main.async {
                    self.imageView.image = self.selectedImage
                }
            }
        }
然后像这样应用滤镜(模糊滤镜)
@IBAction func sliderValueChanged(_ sender: UISlider) {
      if sender.value == 0 {
            _ = allFilters.removeValue(forKey: kCIGaussianBlur)
        } else {
            allFilters[kCIGaussianBlur] = [kCIInputRadiusKey: sender.value]
        }
        if allFilters[kCICrop]?.first == nil {
            allFilters[kCICrop] = ["inputRectangle": CIVector(cgRect: self.currentImage.extent)]
        }
        applyAvailableFilters()
}

您可能应该持有原始图像的引用,而不仅仅是将过滤器应用于当前在UIImageView中显示的任何图像,因为您应用过滤器然后将输出图像设置为UIImageView,并且无法检索输入图像。另一种选择是保持对CIFilter的引用。为此,您应该声明一个属性:

let noirFilter = CIFilter(name: "CIPhotoEffectNoir")!

你可以在你的apply过滤器中使用这个过滤器,并恢复到原来的方法。

@IBAction func BW_Flt_Bt_Tapped(sender: UIButton) {
    let beginImage = CIImage(image: imagePreview.image!)
    noirFilter.setValue(beginImage, forKey: kCIInputImageKey)
    let filteredImage = noirFilter.valueForKey(kCIOutputImageKey) as! CIImage
    imagePreview.image = UIImage(CIImage: filteredImage)
    self.orgImgBt.hidden = false
    self.orgImgL.hidden = false
    BWFlt.hidden = true
    BWLbl.hidden = true
}
@IBAction func orgImgPressed(sender: UIButton) {
    let inputImage = noirFilter.valueForKey(kCIInputImageKey) as! CIImage
    imagePreview.image = UIImage(CIImage: inputImage)
    orgImgBt.hidden = true
    orgImgL.hidden = true
   `enter code here` BWFlt.hidden = false
   `enter code here`BWLbl.hidden = false

    print("button was pressed")
}

或者更理想的:

@IBAction func orgImgPressed(sender: UIButton) {
    imagePreview.image = self.currentInputImage
    orgImgBt.hidden = true
    orgImgL.hidden = true
   `enter code here` BWFlt.hidden = false
   `enter code here`BWLbl.hidden = false

    print("button was pressed")
}

其中currentInputImage是对您正在应用过滤器的UIImage的引用。

相关内容

  • 没有找到相关文章

最新更新