如何从QR码生成器中删除灰色框架

时间:2019-01-06 17:05:39

标签: ios swift uiimage qr-code cifilter

我创建了一个具有不同颜色的QRCode生成器,我想在“框架”中删除“灰色”,而在使用该灰色过滤器后,实际上只有一种白色或纯色 产生一些时间

https://imgur.com/wKEkQ4v enter image description here

let data = string.data(using: .isoLatin1, allowLossyConversion: false)
        if let filter = CIFilter(name: "CIQRCodeGenerator") {


            guard let colorFilter = CIFilter(name: "CIFalseColor") else { return nil }

            filter.setValue(data, forKey: "inputMessage")

            filter.setValue("H", forKey: "inputCorrectionLevel")
            colorFilter.setValue(filter.outputImage, forKey: "inputImage")

            colorFilter.setValue(CIColor(color: UIColor.clear), forKey: "inputColor1")
            colorFilter.setValue(CIColor(color: UIColor.black), forKey: "inputColor0")

 guard let qrCodeImage = colorFilter.outputImage
                else {
                    return nil
            }
            let scaleX = imageView.frame.size.width / qrCodeImage.extent.size.width
            let scaleY = imageView.frame.size.height / qrCodeImage.extent.size.height
            let transform = CGAffineTransform(scaleX: scaleX, y: scaleY)


            if let output = colorFilter.outputImage?.transformed(by: transform) {



                let image = convert(cmage:(output.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))))
                return image

  }
        }
        return nil
}

func convert(cmage:CIImage) -> UIImage
    {
        let context:CIContext = CIContext.init(options: nil)
        let cgImage:CGImage = context.createCGImage(cmage, from: cmage.extent)!
        let image:UIImage = UIImage.init(cgImage: cgImage)
        return image
    }

如果我不使用滤镜来改变颜色,我就不会遇到这个问题 或如果我使用的是蓝色,则我有一帧为浅蓝色

1 个答案:

答案 0 :(得分:1)

使用我发布的链接中的扩展名作为起点:

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
}

enter image description here