快速解码base64字符串出错

时间:2018-08-26 11:02:11

标签: ios swift image base64

上下文如下:我编写了一个应用程序,可以从您的库中选择一个图像,然后将其转换为base64字符串,如下所示:

    let imageData:NSData = UIImageJPEGRepresentation(self.myImageView.image!, 0.1)! as NSData
    let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)

在这里,myImageView是选择图像时存储图像的位置。然后,将其上传到本地mysql数据库中,该数据库具有MEDIUMTEXT类型的列。然后,我编写了一个可以从数据库中获取此字符串的应用程序,但是当我想对其进行解码并再次使其成为UIimage时,我的程序失败了。这是我尝试过的:

    let dataDecoded:NSData = NSData(base64Encoded: tempString64, options: NSData.Base64DecodingOptions(rawValue: 0))!
    let decodedimage:UIImage = UIImage(data: dataDecoded as Data)!

这里tempString64是我从数据库中获得的字符串(我还检查了它是否为空)。如果我运行该应用程序,则在尝试拆包时收到dataDecoded为nil的错误。我真的不明白为什么它为nil,而我的tempString确实在那里。

谢谢。

更新: 我尝试了很多东西,但是没有用!这是我现在在URLSession中使用的代码的相关部分:

   if data != nil {
            do {
                let imagesJSON =  try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSDictionary
                let images : NSArray = imagesJSON["images"] as! NSArray
                let temp : NSArray = images[0] as! NSArray
                var tempString64 : String = temp[0] as! String
                // I found a question on this site that said that the string should be of length multiple of 4, the following does that.
                let remainder = tempString64.count % 4
                if remainder > 0 {
                    tempString64 = tempString64.padding(toLength: tempString64.count + 4 - remainder,
                                                  withPad: "=",
                                                  startingAt: 0)
                }
                //check if string is not nil
                print(tempString64)

                let dataDecoded = Data(base64Encoded: tempString64)
                let decodedimage = UIImage(data: dataDecoded!)

               DispatchQueue.main.async {
                    self.myImageView.image = decodedimage
                }

            } catch {
                print(error)
            }
        }

tempString64的尾巴

wBr9KavclvUrxPlNp6ircQ5qNLTDfe/Sr8Vvj L9KqS0BbEM2AKz2rXmt8j736VRNr/ALX6UJEmcv3qnPSpRZ/N979Kn y8fe/SqKiZhNJV1rP/AGv0pPsf 1 lEkJlFhkYpAoQe9aH2Pj736VE1n/t/pTV7DRXjPzVpL0qGCy5yX/StEW2B979KLFrYzpadafxfhU8ltn L9Kktrbbu b07Uraktn/2Q==

tempString64的长度为13752

1 个答案:

答案 0 :(得分:1)

此问题是由Intent actionIntent = new Intent(this, MainActivity.class); actionIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); actionIntent.setAction(MY_ACTION_ID); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setContentTitle("My notification") .setVisibility(VISIBILITY_PUBLIC) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setAutoCancel(false) .addAction(new NotificationCompat.Action( 0, "My action", PendingIntent.getActivity(this, 0, actionIntent, 0))); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(MY_NOTIFICATION_ID, builder.build()); 选项引起的。如果指定了此选项,则必须使用选项lineLength64Characters

解码数据
ignoreUnknownCharacters

但是省略所有选项会容易得多。该数据库无论如何都不关心漂亮的格式。

let dataDecoded  = Data(base64Encoded: tempString64, options: .ignoreUnknownCharacters)!

更新:

删除填充代码并安全地获取编码的字符串。 API为您处理填充。

在Swift中不要使用let strBase64 = imageData.base64EncodedString() ... let dataDecoded = Data(base64Encoded: tempString64)! NSArray,请使用本机类型安全类型。并且从不永远不要使用NSDictionary,尤其是将值分配给 im 可变常数。该选项在Swift中毫无意义。

.mutableContainers
相关问题