I'm trying to take photo and return image data as return type of function. AVFoundation takes picture asynchronous, and my function returns before it's finished, so I get empty bytes.
I tried calling takePicture() with waiting, as said in this reply Waiting until the task finishes but it just freezes UI.
func takePicture() -> Data {
    var imageData: Data?
        self.stillImageOutput?.captureStillImageAsynchronously(from: videoConnection!, completionHandler: { (imageDataBuffer, error) in
            if let result = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: imageDataBuffer!, previewPhotoSampleBuffer: imageDataBuffer!) {
                imageData = result
            }
            // Removing sessions and stop running
        })
    return imageData!
}
 
     
    