Ios 如何从二维码生成器中删除灰色框架

Ios 如何从二维码生成器中删除灰色框架,ios,swift,uiimage,qr-code,cifilter,Ios,Swift,Uiimage,Qr Code,Cifilter,我用不同的颜色创建了一个QRCode生成器,我想去掉帧中的灰色,在使用滤镜后得到一个白色或透明的颜色 产生一些时间 如果我不使用过滤器来改变颜色,我就不会有这个问题 或者,如果我使用蓝色,我有一个水蓝色的框架,使用我发布的链接的扩展作为起点: extension String { func qrCode(background: UIColor = .white, color: UIColor = .black, output: CGSize = CGSize(width: 250,

我用不同的颜色创建了一个QRCode生成器,我想去掉帧中的灰色,在使用滤镜后得到一个白色或透明的颜色 产生一些时间

如果我不使用过滤器来改变颜色,我就不会有这个问题
或者,如果我使用蓝色,我有一个水蓝色的框架,使用我发布的链接的扩展作为起点:

extension String {
    func qrCode(background: UIColor = .white, color: UIColor = .black, output: CGSize = CGSize(width: 250, height: 250))-> UIImage? {
        guard
            let data = data(using: .isoLatin1),
            let filter = CIFilter(name: "CIQRCodeGenerator")
            else { return nil }
        filter.setValue(data, forKey: "inputMessage")
        filter.setValue("M", forKey: "inputCorrectionLevel")
        guard let image = filter.outputImage
            else { return nil }
        let size = image.extent.integral
        let matrix = CGAffineTransform(scaleX: output.width / size.width, y: output.height / size.height)
        UIGraphicsBeginImageContextWithOptions(output, false, 0)
        defer { UIGraphicsEndImageContext() }
        guard
            let colorFilter = CIFilter(name: "CIFalseColor",
                                   parameters: ["inputImage" : image.transformed(by: matrix),
                                                "inputColor1": CIColor(color: background) ,
                                                "inputColor0": CIColor(color: color)]),
            let coloredImage = colorFilter.outputImage
        else { return nil }
        UIGraphicsBeginImageContextWithOptions(output, false, 0)
        defer { UIGraphicsEndImageContext() }
        UIImage(ciImage: coloredImage).draw(in: CGRect(origin: .zero, size: output))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}


let link = "https://stackoverflow.com/questions/51178573/swift-image-data-from-ciimage-qr-code-how-to-render-cifilter-output?noredirect=1"
if let coloredQRCode = link.qrCode(color: .red, output: CGSize(width: 500, height: 500)) {
    coloredQRCode
}

生成QRCode后,为什么需要使用过滤器
CIFalseColor
?你想达到什么目标?我只需删除CIFalseColor并对qr码过滤器生成的qr码图像应用简单的比例变换。顺便说一句,CIQRCodeGenerator工作代码示例@LeoDabus我想将颜色更改为蓝色qr码或红色等。。。但在所有的二维码框架中,我都遇到了同样的问题frame@LeoDabus我想更改二维码颜色我不明白你的问题,你的图片是白色的,已经有灰色阴影
extension String {
    func qrCode(background: UIColor = .white, color: UIColor = .black, output: CGSize = CGSize(width: 250, height: 250))-> UIImage? {
        guard
            let data = data(using: .isoLatin1),
            let filter = CIFilter(name: "CIQRCodeGenerator")
            else { return nil }
        filter.setValue(data, forKey: "inputMessage")
        filter.setValue("M", forKey: "inputCorrectionLevel")
        guard let image = filter.outputImage
            else { return nil }
        let size = image.extent.integral
        let matrix = CGAffineTransform(scaleX: output.width / size.width, y: output.height / size.height)
        UIGraphicsBeginImageContextWithOptions(output, false, 0)
        defer { UIGraphicsEndImageContext() }
        guard
            let colorFilter = CIFilter(name: "CIFalseColor",
                                   parameters: ["inputImage" : image.transformed(by: matrix),
                                                "inputColor1": CIColor(color: background) ,
                                                "inputColor0": CIColor(color: color)]),
            let coloredImage = colorFilter.outputImage
        else { return nil }
        UIGraphicsBeginImageContextWithOptions(output, false, 0)
        defer { UIGraphicsEndImageContext() }
        UIImage(ciImage: coloredImage).draw(in: CGRect(origin: .zero, size: output))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}


let link = "https://stackoverflow.com/questions/51178573/swift-image-data-from-ciimage-qr-code-how-to-render-cifilter-output?noredirect=1"
if let coloredQRCode = link.qrCode(color: .red, output: CGSize(width: 500, height: 500)) {
    coloredQRCode
}