swift 为什么应用CILinearGradient到CIMaskedVariableBlur会缩小UIImageView的图像?

yhuiod9q  于 2023-03-16  发布在  Swift
关注(0)|答案(1)|浏览(169)

我正在特灵苹果的Selectively Focusing on an Image文章的示例代码。但是UIImageView的图像是过滤器应用后缩小。我不能找出问题在哪里。需要你的帮助。

下面是我的代码:

self.view.addSubview(imageView)
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        imageView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        imageView.widthAnchor.constraint(equalToConstant: 270).isActive = true
        imageView.heightAnchor.constraint(equalToConstant: 480).isActive = true
        
        if let uiImage = UIImage(named: "Image"), let ciImage = CIImage(image: uiImage) {
            let h = ciImage.extent.size.height
            guard let topGradient = CIFilter(name:"CILinearGradient") else {
                return
            }
            topGradient.setValue(CIVector(x:0, y:0.85 * h),
                                 forKey:"inputPoint0")
            topGradient.setValue(CIColor.green,
                                 forKey:"inputColor0")
            topGradient.setValue(CIVector(x:0, y:0.6 * h),
                                 forKey:"inputPoint1")
            topGradient.setValue(CIColor(red:0, green:1, blue:0, alpha:0),
                                 forKey:"inputColor1")
            
            guard let bottomGradient = CIFilter(name:"CILinearGradient") else {
                return
            }
            bottomGradient.setValue(CIVector(x:0, y:0.35 * h),
                                    forKey:"inputPoint0")
            bottomGradient.setValue(CIColor.green,
                                    forKey:"inputColor0")
            bottomGradient.setValue(CIVector(x:0, y:0.6 * h),
                                    forKey:"inputPoint1")
            bottomGradient.setValue(CIColor(red:0, green:1, blue:0, alpha:0),
                                    forKey:"inputColor1")
            
            guard let gradientMask = CIFilter(name:"CIAdditionCompositing") else {
                return
            }
            gradientMask.setValue(topGradient.outputImage,
                                  forKey: kCIInputImageKey)
            gradientMask.setValue(bottomGradient.outputImage,
                                  forKey: kCIInputBackgroundImageKey)
            
            guard let maskedVariableBlur = CIFilter(name:"CIMaskedVariableBlur") else {
                return
            }
            maskedVariableBlur.setValue(ciImage, forKey: kCIInputImageKey)
            maskedVariableBlur.setValue(gradientMask.outputImage, forKey: "inputMask")
            if let selectivelyFocusedCIImage = maskedVariableBlur.outputImage {
                imageView.image = UIImage(ciImage: selectivelyFocusedCIImage)
            }
            
        }

结果:

我特灵将CIMAaskedVariableBlur过滤器应用于我的UIImageView,但UIImageView的图像意外收缩。

kx1ctssn

kx1ctssn1#

这不是真的“缩小”图像......事实上,它是增长图像。因为你把图像视图的contentMode设置为.scaleAspectFill,所以你看不到实际的输出UIImage
除此之外,事情会变得混乱无论如何...
当使用CIFilter并在UIImageCIImage(和/或CGImage)之间转换时,我们必须考虑.extent矩形。
与其试图精确地解释这一点(部分原因是我一开始就不完全清楚),不如让我们来看看如何从(苹果的例子)代码中得到你所期望的。
如果您将最后一个“转换”部分从:

if let selectivelyFocusedCIImage = maskedVariableBlur.outputImage {
    imageView.image = UIImage(ciImage: selectivelyFocusedCIImage)
}

致:

if let selectivelyFocusedCIImage = maskedVariableBlur.outputImage {
    let context = CIContext()
    if let cgimg = context.createCGImage(selectivelyFocusedCIImage, from: ciImage.extent) {
        imageView.image = UIImage(cgImage: cgimg)
    }
}

你应该会得到预期的结果。

相关问题