6

I am using AVFoundation in swift for take pictures but I can't convert any func lines of code from objective c to Swift. My func code is:

 - (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]];
    }
}];

}

This line send me error AnyObject[]does not conform to protocol sequencfe..:

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

In swift:

  for port:AnyObject in connection.inputPorts {

And I don't know how convert this line:

 [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

Can u help me to convert to swift? Thanks!!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3745888
  • 6,143
  • 15
  • 48
  • 97
  • 4
    To learn more about the completion handler and converting them to swift look at this question http://stackoverflow.com/questions/24190277/writing-handler-for-uialertaction/24190415#24190415 – Jacob Jun 18 '14 at 14:26

3 Answers3

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

Arrays of AnyObject should be cast to arrays of your actual type before interating, like this:

for (port in connection.inputPorts as AVCaptureInputPort[]) { }

In terms of blocks to closures, you just have to get the syntax correct.

stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
    (imageSampleBuffer, error) in // This line defines names the inputs
    //...
}

Note that this also uses Trailing Closure Syntax. Do read up on the docs more!

EDIT: In terms of initializers, they now look like this:

let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
self.processImage(UIImage(data:imageData))
Jack
  • 16,677
  • 8
  • 47
  • 51
  • and this??NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; [self processImage:[UIImage imageWithData:imageData]]; how to conveert in swift, I don't know how traduce, thanks! – user3745888 Jun 18 '14 at 15:30
  • great!! but have others errors same type in this method with cast types I have in objective c: CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect); [captureImage setImage:[UIImage imageWithCGImage:imageRef]]; and swift: imageRef = CGImageCreateWithImageInRect(smallImage as? CGImage!, cropRect) and obtain errors type.. imageView2.image.imageWithAlignmentRectInsets(imageRef) thanks! – user3745888 Jun 18 '14 at 16:04
  • @user3745888 I think you should either edit your question to include these or create a new question with what you have and what errors you get. Try to be very clear! – Jack Jun 18 '14 at 16:11
  • 1
    for port in connection.inputPorts as [AVCaptureInputPort] { – Scuttle Jan 01 '15 at 00:12
1

Try this

    var videoConnection :AVCaptureConnection?
   if let videoConnection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo){
     self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (buffer:CMSampleBuffer!, error: NSError!) -> Void in
     if let exifAttachments = CMGetAttachment(buffer, kCGImagePropertyExifDictionary, nil) {
         let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
         self.previewImage.image = UIImage(data: imageData)
         UIImageWriteToSavedPhotosAlbum(self.previewImage.image, nil, nil, nil)
      }
     })
   }
William Hu
  • 15,423
  • 11
  • 100
  • 121
0

This should answer the problem with the ports:

if let videoConnection = stillImageOuput.connectionWithMediaType(AVMediaTypeVideo){//take a photo here}
alexsalo
  • 1,406
  • 2
  • 14
  • 16