如何将代码AVFoundation目标c转换为Swift? [英] How to convert code AVFoundation objective c to Swift?
问题描述
我使用AVFoundation在swift拍照,但我不能将任何func行从客观c转换为Swift。我的功能代码是:
I am using AVFoundation in swift for take pictures but I can't convert any func lines of code from objective c to Swift. My func code is:
- (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]];
}
}];
}
AnyObject []不符合协议sequencfe ..:
This line send me error AnyObject[]does not conform to protocol sequencfe..:
for (AVCaptureInputPort *port in [connection inputPorts]) {
在swift中:
for port:AnyObject in connection.inputPorts {
这行:
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
可以帮我转换成swift吗?
谢谢!
Can u help me to convert to swift? Thanks!!
推荐答案
for (AVCaptureInputPort *port in [connection inputPorts]) { )
数组 AnyObject
在交互之前转换为你的实际类型的数组,像这样:
Arrays of AnyObject
should be cast to arrays of your actual type before interating, like this:
for (port in connection.inputPorts as AVCaptureInputPort[]) { }
对于闭包,只需要获得正确的语法。
In terms of blocks to closures, you just have to get the syntax correct.
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageSampleBuffer, error) in // This line defines names the inputs
//...
}
注意,这也使用跟踪关闭语法。
编辑:在初始化器方面,它们现在看起来像这样:
In terms of initializers, they now look like this:
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
self.processImage(UIImage(data:imageData))
这篇关于如何将代码AVFoundation目标c转换为Swift?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!