UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Encode Image/UIImage To Base64

Forums > SwiftUI

I'm not finding any clarity on this process, but I'm needing to figure out how I might take an image (gathered from the users iPhone Photos via an Image Picker) and convert it to a Base64 String so I can save it to my Database. Here's the front-end:

struct SLPAccountProfileView: View {

    @State var selectedImage: UIImage?
    @State var profileImage: Image?
    @State var imagePickerPresented = false

    var body: some View {
        VStack {
            if profileImage == nil {
                Button(action: { imagePickerPresented = true }, label: {
                    Image("ProfilePlaceholder")
                        .clipShape(Circle())
                        .frame(width: 100, height: 72)
                })

                    .sheet(isPresented: $imagePickerPresented, onDismiss: loadImage, content: {
                        ImagePicker(image: $selectedImage)

                    })
            } else if let image = profileImage {
                VStack {
                    Button(action: { imagePickerPresented = true }, label: {
                        image
                            .resizable()
                            .scaledToFill()
                            .clipped()
                            .clipShape(Circle())
                            .frame(width: 100, height: 72)
                    })
                }
            }
            }
            extension SLPAccountProfileView {
    func loadImage() {
        guard let selectedImage = selectedImage else {return}
        profileImage = Image(uiImage: selectedImage)

    }
}

Basically, its an account profile page where users can select an image and apply it to their profile. Once the image picker sheet dismisses, the loadimage() function runs at the bottom, and assigns their choice to the State variables so it can be displayed locally. I borrowed some of this code so it may already be a little incoherent.

How might I take either the selectedImage: UIImage or profileImage: Image values and use them in a Base64 conversion? This is the closest thing I've found:

https://stackoverflow.com/questions/11251340/convert-between-uiimage-and-base64-string

The problem with the SO post is the source image is a locally stored type, so they have a file name and extension to work with. As I understand it, UIImage doesn't have the convention of a file name for selected iPhone Photos.

3      

The problem with the SO post is the source image is a locally stored type, so they have a file name and extension to work with. As I understand it, UIImage doesn't have the convention of a file name for selected iPhone Photos.

Once you have the image, it doesn't matter where it came from.

I made these two extensions based on the code from Vivek's SO answer:

extension UIImage {
    var base64: String? {
        self.jpegData(compressionQuality: 1)?.base64EncodedString()
    }
}

extension String {
    var imageFromBase64: UIImage? {
        guard let imageData = Data(base64Encoded: self, options: .ignoreUnknownCharacters) else {
            return nil
        }
        return UIImage(data: imageData)
    }
}

let img = //get UIImage from wherever
let base64 = img.base64
let rebornImg = base64?.imageFromBase64

4      

Thanks, this helped a lot!

3      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.