Swift: take photos with AVFoundation

I want to translate this func from obj c to swift, buti cannot translate part of the code. Can someone explain to me how to take a photo with AVFondation or help me translate this function?

- (void) capImage { //method to capture image from AVCaptureSession      video feed
     AVCaptureConnection *videoConnection = nil;
     for (AVCaptureConnection *connection in stillImageOutput.connections) {

  for (AVCaptureInputPort *port in [connection inputPorts]) {

  if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
    videoConnection = connection;
        break;
  }
  }

   if (videoConnection) {
    break;
 }
}

 NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput     captureStillImageAsynchronouslyFromConnection:videoConnection          completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

  if (imageSampleBuffer != NULL) {
   NSData *imageData = [AVCaptureStillImageOutput        jpegStillImageNSDataRepresentation:imageSampleBuffer];
[self processImage:[UIImage imageWithData:imageData]];
 }
}]; }

      

What I did but didn't work:

  func takePhoto(sender:UIButton){

  var videoConnection:AVCaptureConnection
  var connection:AVCaptureConnection
   var port : AVCaptureInputPort

 for connection in stillImageOutput?.connections {

for (port in connection.inputPorts as AVCaptureInputPort) {


    if port = AVMediaTypeVideo {
        videoConnection = connection
        break
    }

}

    if videoConnection {
        break
   }


 }
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection,      completionHandler: {(imageSampleBuffer, error) in
if (imageSampleBuffer != nil) {
    var imageData =      AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer as CMSampleBuffer)
    var image: UIImage = UIImage(data: imageData)


}
})


}

      

Can anyone help me?

+3


source to share


2 answers


Found a good solution:



  func takePhoto(){




if let stillOutput = self.stillImageOutput {
    // we do this on another thread so that we don't hang the UI
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        //find the video connection
        var videoConnection : AVCaptureConnection?
        for connecton in stillOutput.connections {
            //find a matching input port
            for port in connecton.inputPorts!{
                if port.mediaType == AVMediaTypeVideo {
                    videoConnection = connecton as? AVCaptureConnection
                    break //for port
                }
            }

            if videoConnection  != nil {
                break// for connections
            }
        }
        if videoConnection  != nil {
            stillOutput.captureStillImageAsynchronouslyFromConnection(videoConnection){
                (imageSampleBuffer : CMSampleBuffer!, _) in

                let imageDataJpeg = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
                var pickedImage: UIImage = UIImage(data: imageDataJpeg)



            }
            self.captureSession.stopRunning()



        }
    }
}

}

      

+4


source


Since the code above didn't work, I found an elegant solution in Swift.

Instead of this:

var videoConnection : AVCaptureConnection?
for connection in self.stillImageOutput.connections{
    for port in connection.inputPorts!{
        if port.mediaType == AVMediaTypeVideo{
            videoConnection = connection as? AVCaptureConnection
            break //for ports
        }
    }
    if videoConnection != nil{
        break //for connections
    }
}//take a photo then

      



You should use this: (updated for "exit" spelling error)

if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo){//take a photo here}

      

Hope it helps!

+8


source







All Articles