Swift无法从UIImage中删除CIFIlter [英] Swift Cannot remove CIFIlter from UIImage

查看:142
本文介绍了Swift无法从UIImage中删除CIFIlter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在正在研究swift,并且无法解决这个Core Image Framework问题。我能够在图像上成功创建CIFilter但是我不确定如何删除它。图像被放置在类似于Snapchat的相机屏幕的UIView上,然后有一个图像视图,它是预览图像的uiview的子视图。 BTW使用最新版本的Xcode和iOS。

been working on swift for a bit now and having trouble tackling this Core Image Framework. I was able to successfully create a CIFilter over an image however I'm not sure how to delete it. The Image Is placed over a UIView which resembles that of snapchat's camera screen and then there is an imageview which is a subview of the uiview that the image is previewed on. BTW Using The most up to date version of Xcode and iOS as well.

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

Here is the code for when the black and white filter is applied:

@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的子视图

-imagePreview is the subview of the UIView

接下来是我的代码如何更改图像

Next is my code for how to change the image back

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

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

If anyone had any suggestions I would be very appreciative, Thanks!

推荐答案

您可以通过将先前值保留在变量中,或者如果您有多个过滤器将这些值保存在字典。当您应用新过滤器或更改现有过滤器的值时,只需将该值替换为新值并应用原始CIImage上的所有值

You can do this by keeping previous value in a variable or if you have multiple filters keep that values in a dictionary. When you are applying a new filter or changing value of existing filter, just replace that value with new value and apply all values on original 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
                }
            }
        }

然后像这样应用你的过滤器(模糊过滤器)

Then apply your filter like this (Blur filter)

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

这篇关于Swift无法从UIImage中删除CIFIlter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆