Convert between UIImage and Base64 string

后端 未结 24 2874
抹茶落季
抹茶落季 2020-11-22 01:45

Does anyone know how to convert a UIImage to a Base64 string, and then reverse it?

I have the below code; the original image before encoding is good, bu

24条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 02:19

    //convert Image to Base64 (Encoding)
    
    let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
    
    print(strBase64)
    
    // convert Base64 to Image (Decoding)
    
    let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
    
    let decodedimage:UIImage = UIImage(data: dataDecoded)!
    
    print(decodedimage)
    
    yourImageView.image = decodedimage
    

提交回复
热议问题