【问题标题】:Swift : take photo from AVFoundationSwift:从 AVFoundation 拍照
【发布时间】:2014-09-16 14:30:03
【问题描述】:

我想把这个函数从 obj c 翻译成 swift,但是不能翻译一部分代码。 有人可以解释一下如何从 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]];
 }
}]; }

我做了什么但没有工作:

  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)


}
})


}

有人可以帮我吗?

【问题讨论】:

    标签: ios swift avfoundation


    【解决方案1】:

    由于上面的代码不起作用,我在 Swift 中找到了一个优雅的解决方案。

    而不是这个:

    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
    

    你应该使用这个:(更新为“输出”拼写错误)

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

    希望有帮助!

    【讨论】:

      【解决方案2】:

      好的,找到了解决方案:

        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()
      
      
      
              }
          }
      }
      
      }
      

      【讨论】:

      • 由于某种原因对我不起作用。它卡在 stillOutput.connections 中的 for connecton { //为 connecton.inputPorts 中的端口找到匹配的输入端口!{ part/ 你能找到多少个连接?
      猜你喜欢
      • 1970-01-01
      • 2016-08-26
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多