AVCapturePhotoOutput to Base64

292 views Asked by At

I am using swift to develop an app that communicates with my server which runs flask. I would like to be able to take a picture and upload it in a base64 format. Currently the base64 encoding I am using is producing a corrupt and unusable image. As I am using AVCapturePhotoOutput, the UIImage to base64 tutorials have not worked for me as swift is having trouble converting AVCapturePhotoOutput to UIImage. How can I get an base64 image that works reliably? How can I convert the image from AVCapturePhotoOutput to base64? Thanks for any help in advance!

   @IBAction func takePhotoButtonPressed(_ sender: Any) {
          let settings = AVCapturePhotoSettings()
          sessionOutput.capturePhoto(with: settings, delegate: self)
        
    }


    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Swift.Error?) {
        let imageData = photo.fileDataRepresentation()
        var base64String = imageData?.base64EncodedString()
1

There are 1 answers

0
matt On BEST ANSWER

When you configure your photo settings originally, request a preview image by setting the previewPhotoFormat. Now you will be able to get the photo's previewCGImageRepresentation and now you are in a world of pixel data that you'll be able to deal with.