1

I am trying to capture a photo using AVCapturePhotoOutput but the image is getting dark. Here is how I am getting the image

@IBAction func capturephoto(_ sender: Any) {
    print("entered into capture photo")

    let settings = AVCapturePhotoSettings()
    let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
    let previewFormat = [
        kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
        kCVPixelBufferWidthKey as String: self.preivew.frame.width,
        kCVPixelBufferHeightKey as String: self.preivew.frame.height
    ] as [String : Any]
    settings.previewPhotoFormat = previewFormat
    captureSession.addOutput(stillImageOutput)
    print("stillimageoutput is",stillImageOutput)
    self.stillImageOutput.capturePhoto(with: settings, delegate: self)
}


func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

    if let error = error {
        print(error.localizedDescription)
    }
    let imageData = photo.fileDataRepresentation()
    if let data = imageData, let img = UIImage(data: data) {
        print(img)
     }
 }
Sravya Gajavalli
  • 121
  • 1
  • 10

1 Answers1

1

had the same problem and found this on another stack overflow thread, adding it worked for me :)

session.sessionPreset = AVCaptureSession.Preset.photo

and here is the link to discussion

AVCaptureSession is not giving a good photo quality and good resolution

hope this helps.