Swift gives the error that a fileDataRepresentation cannot be created for AVCapturePhoto. Printing "photo", returns the photo's metadata. How can I convert the image to file bytes and eventually base64? I have tried many methods so far, but all rely on obtaining file bytes. Thanks for any responses in advance!
@IBAction func takePhotoButtonPressed(_ sender: Any) {
let settings = AVCapturePhotoSettings()
let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
kCVPixelBufferWidthKey as String: 160,
kCVPixelBufferHeightKey as String: 160]
settings.previewPhotoFormat = previewFormat
settings.isHighResolutionPhotoEnabled = false
sessionOutput.capturePhoto(with: settings, delegate: self)
}
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Swift.Error?) {
let imageData = AVCapturePhoto.fileDataRepresentation()
AVCapturePhoto.fileDataRepresentation is an instance
method, NOT a class
method.
You have to call it on an instance of AVCapturePhoto
like this -
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Swift.Error?) {
let imageData = photo.fileDataRepresentation()
}