AVCaptureVideoDataOutput
現在のセッションに次のように追加する必要があると思います。
AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];
videoOutput.videoSettings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };
[session addOutput:videoOutput];
dispatch_queue_t queue = dispatch_queue_create("MyQueue", NULL);
[videoOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
次に、以下のデリゲート メソッドを実装して、イメージのスナップショットを取得します。
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
UIImage *image = [self imageFromSampleBuffer:sampleBuffer];
// Add your code here that uses the image.
dispatch_async(dispatch_get_main_queue(), ^{
_imageView.image = image;
});
}
これにより、メモリが消費され、アプリのパフォーマンスが低下します。改善するには、次の方法で最適化することもできますAVCaptureVideoDataOutput
。
videoOutput.minFrameDuration = CMTimeMake(1, 15);
も使用できますalwaysDiscardsLateVideoFrames
。