私は、ビデオ静止フレームをその場で処理するiOSアプリケーションを作成しています。これに没頭するために、Appleのexample from the AV* documentationに続きました。AVCaptureDeviceOutputがデリゲートメソッドcaptureOutputを呼び出さない
このプロセスでは、入力(カメラ)と出力を設定します。出力はデリゲートで動作します。この場合、コントローラ自体です(必要なメソッドに準拠して実装されています)。
私が抱えている問題は、デリゲートメソッドが呼び出されないことです。以下のコードはコントローラの実装であり、NSLogが2つあります。私は "開始された"メッセージを見ることができますが、 "委任されたメソッド"は決して表示されません。
このコードはすべて、「AVCaptureVideoDataOutputSampleBufferDelegate」プロトコルを実装するコントローラ内にあります。
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize AV session
AVCaptureSession *session = [AVCaptureSession new];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
[session setSessionPreset:AVCaptureSessionPreset640x480];
else
[session setSessionPreset:AVCaptureSessionPresetPhoto];
// Initialize back camera input
AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:camera error:&error];
if([session canAddInput:input]){
[session addInput:input];
}
// Initialize image output
AVCaptureVideoDataOutput *output = [AVCaptureVideoDataOutput new];
NSDictionary *rgbOutputSettings = [NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:kCMPixelFormat_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
[output setVideoSettings:rgbOutputSettings];
[output setAlwaysDiscardsLateVideoFrames:YES]; // discard if the data output queue is blocked (as we process the still image)
//[output addObserver:self forKeyPath:@"capturingStillImage" options:NSKeyValueObservingOptionNew context:@"AVCaptureStillImageIsCapturingStillImageContext"];
videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
[output setSampleBufferDelegate:self queue:videoDataOutputQueue];
if([session canAddOutput:output]){
[session addOutput:output];
}
[[output connectionWithMediaType:AVMediaTypeVideo] setEnabled:YES];
[session startRunning];
NSLog(@"started");
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
NSLog(@"delegate method called");
CGImageRef cgImage = [self imageFromSampleBuffer:sampleBuffer];
self.theImage.image = [UIImage imageWithCGImage: cgImage ];
CGImageRelease(cgImage);
}
注:私は、ターゲットとしてのiOS 5.0で構築しています。
編集:私は別の問題への解決策を求めたが、私のコードが行うことになっている正確に何やっていること、questionを見つけた
。私はその問題のコードを空のxcodeアプリにコピーし、captureOutput関数にNSLogsを追加し、呼び出されません。これは構成の問題ですか?私は行方不明のものがありますか?
セッションの実行中にエラーが発生した場合(フレームが取得されていない可能性が高いため)、AVCaptureSessionRuntimeErrorNotification通知を送信します。 '[NSNotificationCenter defaultCenter] addObserver:selector:name:object:];を使用してそれを聞いてください。セレクタが呼ばれたら、ユーザ辞書から' AVCaptureSessionErrorKey'を取得してエラーを見てください。 – lnafziger
入力いただきありがとうございます@Inafziger。私はAVCaptureSessionRuntimeErrorNotificationを許可しましたが、トリガーされていないようです: – SuitedSloth
どのようにView Controllerが作成されるのですか? 「開始しましたか」が出力されますか? – Tommy