Search code examples
iosswiftbase64png

Reduce base64 encoded string size


I am using the following method to save the PNG image to UserDefaults:

func convertImageToBase64String (img: UIImage) -> String {
    return img.pngData()?.base64EncodedString() ?? ""
}

The issues are that this data is using significantly Documents & Data storage: enter image description here

And for some reason even if this data is removed from UserDefaults, this amount do not change.

I would like to fix this issues, but need to use PNG images as transparent background is important.


Solution

  • After some research, I found out PNG files were not using significant storage. The issue was that original images were being stored in temporary data. I managed to fix this issue following this suggestion: https://stackoverflow.com/a/37043842/14880207

    I followed Ramesh Sanghar suggestion as well and stored images in the way he suggested.