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, but I only get a blank image after I encode and decode it.
NSData *imageData = UIImagePNGRepresentation(viewImage);
NSString *b64EncStr = [self encode: imageData];
NSString *base64String = [self encodeBase64:imageData];
In Swift 3.0 and Xcode 8.0
Encoding :
Decoding :
In Swift 3.0
Swift 3.0 and Xcode 8.0
Swift 4.2 Extension method
XCode 9.1 and Swift 4.0
Swift version - create base64 for image
In my opinion Implicitly Unwrapped Optional in case of
UIImagePNGRepresenatation()
is not safe, so I recommend to use extension like below:Swift 4