0
我找到了一個在Swift中編寫的AVFoundation示例代碼。我試圖將代碼更改爲Objective-C。但我不確定是否正確執行此操作,因爲此時代碼不起作用。你可以看看嗎?AVFoundation將Swift代碼更改爲Objective-C
func initialiseCaptureSession()
{
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
guard let frontCamera = (AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice])
.filter({ $0.position == .Front })
.first else
{
fatalError("Unable to access front camera")
}
do
{
let input = try AVCaptureDeviceInput(device: frontCamera)
captureSession.addInput(input)
}
catch
{
fatalError("Unable to access front camera")
}
let videoOutput = AVCaptureVideoDataOutput()
videoOutput.setSampleBufferDelegate(self, queue: dispatch_queue_create("sample buffer delegate", DISPATCH_QUEUE_SERIAL))
if captureSession.canAddOutput(videoOutput)
{
captureSession.addOutput(videoOutput)
}
captureSession.startRunning()
}
我的Objective-C代碼:
-(void)initializeCaptureSession {
self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
AVCaptureDevice *inputDevice = nil;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *deviceInput;
for(AVCaptureDevice *camera in devices) {
if([camera position] == AVCaptureDevicePositionFront) { // is front camera
inputDevice = camera;
deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:nil];
[self.captureSession addInput:deviceInput];
break;
}
}
AVCaptureVideoDataOutput *videoOutput = nil;
[videoOutput setSampleBufferDelegate:self queue:dispatch_queue_create("sample buffer delegate", DISPATCH_QUEUE_SERIAL)];
if([self.captureSession canAddOutput:videoOutput]) {
[self.captureSession addOutput:videoOutput];
}
[self.captureSession startRunning];
}
的問題是,在我的情況
if([self.captureSession canAddOutput:videoOutput]) { [self.captureSession addOutput:videoOutput]; }
被忽略。我不確定,但是我的self.captureSession addInput:deviceInput];?
最好的問候, 納扎爾
感謝兄弟。這工作!我也試着用** AVCaptureDevice * inputDevice = [[AVCaptureDevice alloc] init]來初始化** AVCaptureDevice * inputDevice = nil; **; **但這造成了一個例外。我是否也必須初始化? –