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.