Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

jean-marie castellucci
jean-marie castellucci
4,954 Points

Swift : take photo from AVFondation

Hi

I want to translate this func from obj c to swift :

  - (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 not working :

   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)


    }
})


}

Could someone help me ?

2 Answers

jean-marie castellucci
jean-marie castellucci
4,954 Points

Thanks but it doesn't worked.

Finaly i found the 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)

                let scaledImage:UIImage = self.squareImageFromImage(pickedImage, newSize: 320.0)
                //let scaledImage = self.imageWithImage(pickedImage, scaledToSize: CGSizeMake(320, 320))

                let imageData = UIImagePNGRepresentation(scaledImage)
                var imageFile:PFFile = PFFile(data: imageData)

                var userPost = PFObject(className:"UserPost")

                userPost["imageFile"] = imageFile
                userPost["from"] = PFUser.currentUser()
                userPost.saveInBackground()


            }
            self.captureSession.stopRunning()
        }
    }
}
println("take photo pressed")
}
Naftali Beder
Naftali Beder
927 Points

I get an error when I use this code that says:

"Camera" does not have a member named "stillImageOutput".

It seems like I need to define that somewhere else. Any explanation would be very helpful. Thank you!

Stone Preston
Stone Preston
42,016 Points

one thing i see is this

if port = AVMediaTypeVideo {
            videoConnection = connection
            break
        }

you probably need to compare using ==

if port == AVMediaTypeVideo {
            videoConnection = connection
            break
        }