如何使用UISlider更改CIFilter的强度?

时间:2018-08-22 18:10:07

标签: swift uiimage uislider cgimage cifilter

我有一个UISlider,可在其中访问“ var currentFilter:CIFilter!”。并设置强度。

    @IBAction func amountSliderValueChanged(_ slider: UISlider) {

        let inputKeys = currentFilter.inputKeys

        if inputKeys.contains(kCIInputIntensityKey) { currentFilter.setValue(intensity.value, forKey: kCIInputIntensityKey) }
        if inputKeys.contains(kCIInputRadiusKey) { currentFilter.setValue(intensity.value * 200, forKey: kCIInputRadiusKey) }
        if inputKeys.contains(kCIInputScaleKey) { currentFilter.setValue(intensity.value * 10, forKey: kCIInputScaleKey) }
        if inputKeys.contains(kCIInputCenterKey) { currentFilter.setValue(CIVector(x: imagePassed.size.width / 2, y: imagePassed.size.height / 2), forKey: kCIInputCenterKey) }

}

我通过了'var imagePassed:UIImage!'进入uislider以访问图像。

在“ scrollFunctions”内部管理过滤器,将其应用于UIScrollView。

    func ScrollFunctions() {
        self.imgView.image = rotateImage(image: imagePassed)
        var xCoord: CGFloat = 13
        let yCoord: CGFloat = 7
        let buttonWidth:CGFloat = 60
        let buttonHeight: CGFloat = 60
        let gapBetweenButtons: CGFloat = 5
        var itemCount = 0

        for i in 0..<CIFilterNames.count {
            itemCount = i

            // Button properties

            let filterButton = UIButton(type: .custom)
            filterButton.frame = CGRect(xCoord, yCoord, buttonWidth, buttonHeight)
            filterButton.tag = itemCount
            filterButton.addTarget(self, action: #selector(LooksViewController.filterButtonTapped(sender:)), for: .touchUpInside)
            filterButton.layer.cornerRadius = 0
            filterButton.clipsToBounds = true
            let originalOrientation: UIImageOrientation = imgView.image!.imageOrientation
            let originalScale: CGFloat = imgView.image!.scale

            // Create filters for each button

//            var newPtImage = UIImage(cgImage: filteredImageRef, scale: originalScale, orientation: originalOrientation)

            currentFilter = CIFilter(name: "\(CIFilterNames[i])" )
            let ciContext = CIContext(options: nil)
            let coreImage = CIImage(image: imagePassed)
            let filter = CIFilter(name: "\(CIFilterNames[i])" )
            filter!.setDefaults()
            filter!.setValue(coreImage, forKey: kCIInputImageKey)
            let filteredImageData = filter!.value(forKey: kCIOutputImageKey) as! CIImage
            let filteredImageRef = ciContext.createCGImage(filteredImageData, from: filteredImageData.extent)
//            var imageForButton = UIImage(cgImage: filteredImageRef!);
            var imageForButton = UIImage(cgImage: filteredImageRef!, scale: originalScale, orientation: originalOrientation)

            // Assign filtered image to the button
            imageForButton = rotateImage(image: imageForButton)
            filterButton.setBackgroundImage(imageForButton, for: .normal)
            // Add Buttons in the Scroll View
            xCoord +=  buttonWidth + gapBetweenButtons
            filtersScrollView.addSubview(filterButton)

        }

        // Resize Scroll View
        filtersScrollView.contentSize = CGSize(buttonWidth * CGFloat(itemCount+2), yCoord)

        }

当我移动滑块时,它不会改变滤镜的强度。有任何想法吗?以下是过滤器名称:

var CIFilterNames = [
    "CIPhotoEffectChrome",
    "CIPhotoEffectFade",
    "CIPhotoEffectInstant",
    "CIPhotoEffectNoir",
    "CIPhotoEffectProcess",
    "CIPhotoEffectTonal",
    "CIPhotoEffectTransfer",
    "CISepiaTone"
]

0 个答案:

没有答案