简体   繁体   English

斯威夫特:从AVFoundation拍照

[英]Swift : take photo from AVFoundation

I want to translate this func from obj c to swift, buti can't translate a part of the code. 我想将这个函数从obj c转换为swift,buti无法翻译部分代码。 Could someone explain me how to take photo from AVFondation or help me to translate this function ? 有人可以解释我如何从AVFondation拍照或帮我翻译这个功能?

- (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 ? 有人能帮助我吗?

Since code above didn't work I found an elegant solution in Swift. 由于上面的代码不起作用,我在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 "output" spelling error) 您应该使用此:(更新为“输出”拼写错误)

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

Hope that helps! 希望有所帮助!

Ok 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)



            }
            self.captureSession.stopRunning()



        }
    }
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM