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