Converting Image to BASE64 String in swift

Actually it will not take time to convert but for printing, it will take more time so don't print it...


You can apply this code

let imageData: Data? = UIImageJPEGRepresentation(getImage(), 0.4)
let imageStr = imageData?.base64EncodedString(options: .lineLength64Characters) ?? ""
print(strBase64)

Swift 4 version. This simple func worked well for me. Confirmed decoded image back using this: https://codebeautify.org/base64-to-image-converter Hope this helps someone.

public static func  convertImageToBase64String(image : UIImage ) -> String 
{
    let strBase64 =  image.pngData()?.base64EncodedString()
    return strBase64!
}