Adopting these protocols lets implementations of the Encoder and Decoder protocols take your data and encode or decode it to and from an external. You adopt this approach by implementing the Encodable and Decodable protocols on your custom types. The Base64 encoded string will be printed out to the console. The Swift standard library defines a standardized approach to data encoding and decoding. You can copy it and paste it into the Xcode Playground project to see how it works. MyImageView.image = UIImage(data: newImageData)īelow is a complete code example in Swift. Once we have the Data object we can use it to create UIImage let newImageData = Data(base64Encoded: imageBase64String!) let newImageData = Data(base64Encoded: imageBase64String!) The encoded Base64 string can be decoded back into a Data object. Print(imageBase64String ? "Could not encode image to Base64") Create UIImage From Base64 String Let imageBase64String = imageData?.base64EncodedString() Convert image Data to base64 encodded string To encode image Data into the Base64 string we need to use the base64EncodedString() method of a Data object. Let imageData = catImage?.jpegData(compressionQuality: 1)
![xcode text encoding xcode text encoding](https://www.kindacode.com/wp-content/uploads/2020/12/Screen-Shot-2020-12-02-at-03.11.28.jpg)
Use jpegData(compressionQuality: 1) or jpegData(compressionQuality: 0)to get data of JPG image.Use pngData() to get data of PNG image,.Depending on the image type, we can get image PNG or JPG data. Now when an image is created and is displayed in the app, we can grab the image data and encode it to the Base64 string. View.addSubview(myImageView) Encode Image Data to Base64 String
![xcode text encoding xcode text encoding](http://www.web3.lu/wp-content/uploads/2015/03/xcode_textedit.jpg)
Let myImageView:UIImageView = UIImageView() To make an image display in the app I can use the following Swift code snippet. To create a new UIImage in Swift I can add an image file to my Xcode project and then create a new image using this image file name.
#XCODE TEXT ENCODING HOW TO#
If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App Create UIImageīefore I can encode existing image data into the Base64 string, I will need to have UIImge create.