【问题标题】:How to decode a base 64 string found in a JSON string to generate a UIImage?如何解码在 JSON 字符串中找到的 base 64 字符串以生成 UIImage?
【发布时间】:2018-07-27 10:34:07
【问题描述】:

我在下面粘贴了我的代码。从本质上讲,我得到了一个 SDK,它有一个 JSON 字符串,然后我必须对其进行解析,以便获取生成 QR 码的 base64 字符串。我一直在努力,但代码在“let nsd = ...”行出现错误,并显示消息:“线程 1:致命错误:在展开可选值时意外发现 nil”

如果我出错了,我们将不胜感激。在谈到 Swift 和一般编程时,我是一个新手,所以我觉得这很有挑战性。我也不认为我正确地将响应转换为 JSON,因为这是它第一次出错的地方。

func qrCodeGenerator(payload : String) {

    guard let response = /response as a string from SDK/ else {return}


    /* convert response string to an NSData response, so as to convert to JSON in the code below */
    let nsd: NSData = NSData(base64Encoded: response, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!

    var jsonResponse = JSON.null

    do {

        /* convert the response to a json object */
        try jsonResponse = JSONSerialization.jsonObject(with: nsd as Data, options: []) as! JSON

        /* enter the result array, as the base64 string is contained there */
        var result = jsonResponse["result"][0]
        var resqr_64 = result["qr_b64"].stringValue

        print(resqr_64)

        var base64string = resqr_64

        /*The base64 string lies beyond the comma*/
        var base64image = String(base64string.split(separator: ",")[1]) as String

        var decodeString : NSData = NSData(base64Encoded: base64image, options: [])!
        var decodedimage: UIImage = UIImage(data: decodeString as Data)!

        QRCodeImageView.image = decodedimage
    } catch {
        print(error)
    }
}

任何帮助将不胜感激!非常感谢。

【问题讨论】:

    标签: json swift base64 decode qr-code


    【解决方案1】:
     if let decodedData = Data(base64Encoded: (dataDict["THUMBNAIL"] as? String)! , options: .ignoreUnknownCharacters{
    
                self.imgThumb.image = UIImage(data: decodedData)
    
            }
    

    尝试使用此代码从 base64 字符串获取图像 :)

    【讨论】:

      【解决方案2】:

      代码中有两个严重的问题:

      1) 响应很可能是常规 JSON 字符串(不是 base64 编码的),无法从字符串初始化 base64 编码的Data
      2) 您不能强制将JSONSerialization 的结果转换为JSON,请使用SwiftyJSON 初始化程序。

      请尝试

      func qrCodeGenerator(payload : String) {
      
          guard let response = /response as a string from SDK/ else {return}
      
      
          /* convert response string to an NSData response, so as to convert to JSON in the code below */
          let data = Data(response.utf8)
      
          do {
      
              /* convert the response to a json object */
              let jsonResponse = try JSON(data)
      
              /* enter the result array, as the base64 string is contained there */
              let result = jsonResponse["result"][0]
              let base64string = result["qr_b64"].stringValue
      
              print(base64string)
      
              /*The base64 string lies beyond the comma*/
              let base64image = String(base64string.split(separator: ",")[1])
      
              let decodeString = Data(base64Encoded: base64image)!
              let decodedimage = UIImage(data: decodeString)!
      
              QRCodeImageView.image = decodedimage
          } catch {
              print(error)
          }
      }
      

      注意:你不关心一堆 Variable 'xyz' was never mutated; consider changing to 'let' constant 警告吗? ?

      【讨论】:

      • 非常感谢!这真的很有帮助。
      猜你喜欢
      • 1970-01-01
      • 2018-03-19
      • 2012-11-07
      • 2021-10-14
      • 2017-06-17
      • 2012-04-10
      • 2023-03-26
      • 2015-08-09
      • 2017-02-03
      相关资源
      最近更新 更多