If you've ever dealt with data that's been encoded in Base64, you may have wondered how to decode it back into its original format or even how to encode a string or image to Base64. Luckily, Swift provides built-in support for encoding and decoding Base64 data.
Base64 is a way of representing binary data as ASCII text. It's often used in email attachments, web pages, and other applications where binary data needs to be transmitted over channels that only support ASCII characters. Base64 encodes 3 bytes of data into 4 bytes of ASCII text. The resulting encoded text can then be transmitted without any loss of data.
To encode a string or an image to Base64, we first need to convert it to Data
and then call the base64EncodedString()
method on it. Here's an example of encoding a string to Base64:
1let message = "Hello, world!"2if let data = message.data(using: .utf8) {3 let base64encoded = data.base64EncodedString()4 print(base64encoded)5}
The output of this code will be:
1SGVsbG8sIHdvcmxkIQ==
Here's another example of encoding an image to Base64:
1let image = UIImage(named: "myImage")2if let imageData = image.pngData() {3 let base64encoded = imageData.base64EncodedString()4 print(base64encoded)5}
To decode a Base64 string back into its original format, we need to call the base64EncodedData()
method on the decoded string. This method returns a Data
object that can be used to construct the original object. Here's an example of decoding a Base64 string back into a string:
1let base64string = "SGVsbG8sIHdvcmxkIQ=="2if let data = Data(base64Encoded: base64string) {3 if let message = String(data: data, encoding: .utf8) {4 print(message)5 }6}
The output of this code will be:
1Hello, world!
Here's another example of decoding a Base64 string back into an image:
1let base64string = "iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKTWlDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFcUFP2fjHT/uzZI6GgYcL+MqzJWlpFy9jJlZCjoKLGvq3ivq+ALFJgJKTBhluwspQOJkRm5hQrJtrLf39j7vPe/Pe773u+7/fw+BhYGEhYaGiImKi4yMjgyMjw9PT4/QEJDR0ZHSElKS0xNTk9RUVFVVldYWVpjZGtqaW5ucXJzdHV2d3h5ent8fX6AgYKDhIWGh4iJiouMjY2TlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLExcvP0NDS0tPXF1drCwAAACFklEQVRo3s2bS26DMAxFQQQxRgIaBBAihRYIrUBHq3v//eXIcszL1/u9QNp+J4IvAz8cPyW5H5Wh5VL5o5SvD8jOVyxeU9QrTy6VTZUck7ZJ6Wkr1V7wzVD0mBr6plskz6Ge64A9NZlwHddAq3TJALZ+a0gd/ct0NVsHsOyE31MXnzN/adPn9VoCFrtIn this example, we decode a Base64 string and convert it into an `UIImage` object:2
3```swift4let base64string = "iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKTWlDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFcUFP2fjHT/uzZI6GgYcL+MqzJWlpFy9jJlZCjoKLGvq3ivq+ALFJgJKTBhluwspQOJkRm5hQrJtrLf39j7vPe/Pe773u+7/fw+BhYGEhYaGiImKi4yMjgyMjw9PT4/QEJDR0ZHSElKS0xNTk9RUVFVVldYWVpjZGtqaW5ucXJzdHV2d3h5ent8fX6AgYKDhIWGh4iJiouMjY2TlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLExcvP0NDS0tPXF1drCwAAACFklEQVRo3s2bS26DMAxFQQQxRgIaBBAihRYIrUBHq3v//eXIcszL1/u9QNp+J4IvAz8cPyW5H5Wh5VL5o5SvD8jOVyxeU9QrTy6VTZUck7ZJ6Wkr1V7wzVD0mBr6plskz6Ge64A9NZlwHddAq3TJALZ+a0gd/ct0NVsHsOyE31MXnzN/adPn9VoCFrt"5if let data = Data(base64Encoded: base64string) {6 if let image = UIImage(data: data) {7 // Use the `image` object8 }9}