I have a photo taking app that is using AVFoundation. So far everything works perfectly.
However, the one thing that is really confusing me is, what object is the captured image actually contained in?
I have been NSLogging all of the objects and some of their properties and I still can't figure out where the captured image is contained.
Here is my code for setting up the capture session:
self.session =[[AVCaptureSession alloc]init];
[self.session setSessionPreset:AVCaptureSessionPresetPhoto];
self.inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
self.deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:self.inputDevice error:&error];
if([self.session canAddInput:self.deviceInput])
[self.session addInput:self.deviceInput];
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];
self.rootLayer = [[self view]layer];
[self.rootLayer setMasksToBounds:YES];
[self.previewLayer setFrame:CGRectMake(0, 0, self.rootLayer.bounds.size.width, self.rootLayer.bounds.size.height)];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.rootLayer insertSublayer:self.previewLayer atIndex:0];
self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
[self.session addOutput:self.stillImageOutput];
[self.session startRunning];
}
And then here is my code for capturing a still image when the user presses the capture button:
-(IBAction)stillImageCapture {
AVCaptureConnection *videoConnection = nil;
videoConnection.videoOrientation = AVCaptureVideoOrientationPortrait;
for (AVCaptureConnection *connection in self.stillImageOutput.connections){
for (AVCaptureInputPort *port in [connection inputPorts]){
if ([[port mediaType] isEqual:AVMediaTypeVideo]){
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
[self.session stopRunning];
}
];}
When the user presses the capture button, and the above code executes, the captured image is successfully displayed on the iPhone screen, but I can't figure out which object is actually holding the captured image.
Thanks for the help.
The
CMSampleBuffer
is what actually contains the image.In your
captureStillImageAsynchronouslyFromConnection
completion handler, you'll want something like:My working implementation of it: