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
Encoding
func convertImageToBase64String (img: UIImage) -> String {
return img.jpegData(compressionQuality: 1)?.base64EncodedString() ?? ""
}
Decoding
func convertBase64StringToImage (imageBase64String:String) -> UIImage {
let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0))
let image = UIImage(data: imageData!)
return image!
}
Note: Tested in xcode 10.2
Encoding
func convertImageToBase64String (img: UIImage) -> String {
let imageData:NSData = UIImageJPEGRepresentation(img, 0.50)! as NSData //UIImagePNGRepresentation(img)
let imgString = imageData.base64EncodedString(options: .init(rawValue: 0))
return imgString
}
Decoding
func convertBase64StringToImage (imageBase64String:String) -> UIImage {
let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0))
let image = UIImage(data: imageData!)
return image
}
Note: Tested in xcode 9.4.1