2014-09-16 4 views
3

이 func을 obj c에서 swift로 변환하고 싶습니다. buti는 코드의 일부를 번역 할 수 없습니다. 누군가가 AVFondation에서 사진을 찍는 방법을 설명하거나이 기능을 번역하는 데 도움이 될 수 있습니까?스위프트 : AVFoundation에서 사진 찍기

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


} 
}) 


} 

누군가가 나를 도울 수 있을까요? 위의 코드는 작동하지 않았기 때문에

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

확인 솔루션을 발견했다. 대신이의

:

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} 

희망 ("출력"맞춤법 오류 업데이트)!

+0

어떤 이유로 든 작동하지 않습니다. stillOutput.connections에있는 connecton에 붙어 있습니다. { // connecton.inputPorts에서 일치하는 입력 포트를 찾습니다. ! {part/얼마나 많은 연결을 찾을 수 있었습니까? – alexsalo

8

내가 스위프트의 우아한 해결책을 발견 :

+0

이것은 나에게도 효과가있다. 고마워! – Fengson