AVCaptureDeviceOutput not calling delegate method captureOutput
Your session
is a local variable. Its scope is limited to viewDidLoad
. Since this is a new project, I assume it's safe to say that you're using ARC. In that case that object won't leak and therefore continue to live as it would have done in the linked question, rather the compiler will ensure the object is deallocated before viewDidLoad
exits.
Hence your session isn't running because it no longer exists.
(aside: the self.theImage.image = ...
is unsafe since it performs a UIKit action of the main queue; you probably want to dispatch_async
that over to dispatch_get_main_queue()
)
So, sample corrections:
@implementation YourViewController
{
AVCaptureSession *session;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize AV session
session = [AVCaptureSession new];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
[session setSessionPreset:AVCaptureSessionPreset640x480];
else
/* ... etc ... */
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
NSLog(@"delegate method called");
CGImageRef cgImage = [self imageFromSampleBuffer:sampleBuffer];
dispatch_sync(dispatch_get_main_queue(),
^{
self.theImage.image = [UIImage imageWithCGImage: cgImage ];
CGImageRelease( cgImage );
});
}
Most people advocate using an underscore at the beginning of instance variable names nowadays but I omitted it for simplicity. You can use Xcode's built in refactor tool to fix that up after you've verified that the diagnosis is correct.
I moved the CGImageRelease
inside the block sent to the main queue to ensure its lifetime extends beyond its capture into a UIImage
. I'm not immediately able to find any documentation to confirm that CoreFoundation objects have their lifetime automatically extended when captured in a block.
I've found one more reason why didOutputSampleBuffer
delegate method may not be called — save to file and get sample buffer output connections are mutually exclusive. In other words, if your session already has AVCaptureMovieFileOutput
and then you add AVCaptureVideoDataOutput
, only AVCaptureFileOutputRecordingDelegate
delegate methods are called.
Just for the reference, I couldn't find anywhere in AV Foundation
framework documentation explicit description of this limitation, but Apple support confirmed this a few years ago, as noted in this SO answer.
One way to solve the problem is to remove AVCaptureMovieFileOutput
entirely and manually write recorded frames to the file in didOutputSampleBuffer
delegate method, alongside your custom buffer data processing. You may find these two SO answers useful.