Swift base64 decoding returns nil

后端 未结 7 901
-上瘾入骨i
-上瘾入骨i 2020-12-15 06:36

I am trying to decode a base64 string to an image in Swift using the following code:

let decodedData=NSData(base64EncodedString: encodedImageData, options: N         


        
7条回答
  •  暖寄归人
    2020-12-15 06:53

    (Swift 3) I been in this situation, Trying to get the data using base64 encoded string returns nil with me when I used this line

    let imageData = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)
    

    Tried padding the string and didn't work out too

    This is what worked with me

    func imageForBase64String(_ strBase64: String) -> UIImage? {
    
        do{
            let imageData = try Data(contentsOf: URL(string: strBase64)!)
            let image = UIImage(data: imageData)
            return image!
        }
        catch{
            return nil
        }
    }
    

提交回复
热议问题