【问题标题】:How do I clearly display the QR code in swift5? [duplicate]如何在swift5中清晰显示二维码? [复制]
【发布时间】:2019-10-15 15:19:43
【问题描述】:

我正在制作二维码屏幕。但我的问题是二维码看起来太模糊了。我想看清楚。我该怎么办?

当前使用情况

        // Get data from the string
        let data = myString.data(using: String.Encoding.ascii)
        // Get a QR CIFilter
        guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return }
        // Input the data
        qrFilter.setValue(data, forKey: "inputMessage")
        // Get the output image
        guard let qrImage = qrFilter.outputImage else { return }
        // Do some processing to get the UIImage
        let context = CIContext()
        guard let cgImage = context.createCGImage(qrImage, from: qrImage.extent) else { return }
        self.qrcdoeView.image = UIImage(cgImage: cgImage)

我只是看了一下示例并照做了。

【问题讨论】:

  • this 回答你的问题了吗?
  • 嗨@Sweeper 这不是Object-C的问题吗?我使用 Swift5。
  • 该页面上也有快速的答案。这个问题不是专门针对 Objective-C 语言的。
  • @Sweeper 对不起。我没有仔细看。我再次阅读了您推荐的页面并阅读了答案。谢谢,但我已经通过参考下面的答案解决了这个问题。谢谢你帮助我。
  • 您应该使用.isoLatin1 而不是.ascii 根据 ISO/IEC 18004:2006 标准生成表示输入数据的输出图像。输出图像中代码的每个模块(方点)的宽度和高度为一个点。要从字符串或 URL 创建 QR 码,请使用 NSISOLatin1StringEncoding 字符串编码将其转换为 NSData 对象。

标签: ios swift qr-code


【解决方案1】:

朋友试试这个。

 func generateQRCode(from string: String) -> UIImage? {
        let data = string.data(using: String.Encoding.ascii)

        if let filter = CIFilter(name: "CIQRCodeGenerator") {
            filter.setValue(data, forKey: "inputMessage")
            let transform = CGAffineTransform(scaleX: 3, y: 3)

            if let output = filter.outputImage?.transformed(by: transform) {
                return UIImage(ciImage: output)
            }
        }

        return nil
    }

    let image = generateQRCode(from: "Game of Thrones")

【讨论】:

  • 《权力的游戏》这部分是二维码的价值吗?
  • "Game a Thrones" 是将在 QRCode 中编码的值
猜你喜欢
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 2015-09-17
  • 1970-01-01
  • 2020-11-10
相关资源
最近更新 更多