ios - 无法从生成的 QRCode 中获取信息

标签 ios swift swift3 avfoundation

我正在尝试生成包含多个字符串的 QRCode。它正在工作,但是生成的图像 QRCode 在 imageView 中太小,因此无法读取它(至少我认为这就是为什么我无法从中获取信息的原因)。

它是这样的:

enter image description here

然后像这样生成它:

func generateQRWithInfo(){
    var aggregateData = [String: NSData]()

    if let firstName = firstName?.data(using: String.Encoding.isoLatin1, allowLossyConversion: false) {
        aggregateData.updateValue(firstName as NSData, forKey: "firstName")

    }

    if let lastName = lastName?.data(using: String.Encoding.isoLatin1, allowLossyConversion: false) {
        aggregateData.updateValue(lastName as NSData, forKey: "lastName")
    }

    if let job = job?.data(using: String.Encoding.isoLatin1, allowLossyConversion: false) {
        aggregateData.updateValue(job as NSData, forKey: "job")
    }

    if let organization = organization?.data(using: String.Encoding.isoLatin1, allowLossyConversion: false) {
        aggregateData.updateValue(organization as NSData, forKey: "organization")
    }

    if let mobilePhone = mobilePhone?.data(using: String.Encoding.isoLatin1, allowLossyConversion: false) {
        aggregateData.updateValue(mobilePhone as NSData, forKey: "mobilePhone")
    }

    if let workPhone = workPhone?.data(using: String.Encoding.isoLatin1, allowLossyConversion: false) {
        aggregateData.updateValue(workPhone as NSData, forKey: "workPhone")
    }

    if let email = email?.data(using: String.Encoding.isoLatin1, allowLossyConversion: false) {
        aggregateData.updateValue(email as NSData, forKey: "email")
    }

    let archived = NSKeyedArchiver.archivedData(withRootObject: aggregateData)
    let filter = CIFilter(name: "CIQRCodeGenerator")

    filter?.setValue(archived, forKey: "inputMessage")
    filter?.setValue("Q", forKey: "inputCorrectionLevel")

    let qrCodeImage = filter?.outputImage
    let context = CIContext(options: nil)
    //let cgImage = context.createCGImage(qrCodeImage!, from: (qrCodeImage?.extent)!)

    let transform = CGAffineTransform(scaleX: 50,y: 50)

    let output = filter?.outputImage?.applying(transform)

    let newImage = UIImage(ciImage: output!)

    qrImageView.image = newImage
}

我不知道它是否应该如此,但我无法从中获取信息。我做错了什么?

最佳答案

QR 码根据这些参数保存大量数据。

  • 数据类型
  • 大小又名像素
  • 纠错级别

数据类型可以是数字、字母数字和二进制。 根据丢失恢复的可能性,纠错级别可分为 L、M、Q 和 H 类。

所以根据您的情况,您想要生成 30*30 字母数字,所以显然您不能存储超过允许值的值。所以让它更大或减少数据。请注意,所有 QR 码阅读器都不相同。

有关更多信息,请查看此 table

关于ios - 无法从生成的 QRCode 中获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41877483/

相关文章:

android - 通过方法 channel flutter 传递自定义对象

swift3 - 使用 Alamofire 将图像上传到服务器

ios - 如何调试 APIService 的代码

ios - NSCalendar 按年份给出了错误的周数

ios - 使用 UIScreenEdgePanGestureRecognizer 快速平移时,translationInView 等于 0

ios - UITableViewCell 崩溃

ios - Swift 3 中的 PBEWithMD5AndDES 加密

ios - Swift 3 定时事件

iOS 4.2.1 : Video files not being played by MPMoviePlayerController on iPod Touch

iOS8:无法在 iOS8 中添加/显示 subview Controller View