Below is a step-by-step guide on capturing an image using AVFoundation and saving it in a photo album.
Add a UIView object to the NIB (or as a subview) and create in the @property controller:
@property(nonatomic, retain) IBOutlet UIView *vImagePreview;
Connect the UIView to the output above in IB, or assign it directly if you use code instead of NIB.
Then edit your UIViewController and give it the following viewDidAppear method:
-(void)viewDidAppear:(BOOL)animated { AVCaptureSession *session = [[AVCaptureSession alloc] init]; session.sessionPreset = AVCaptureSessionPresetMedium; CALayer *viewLayer = self.vImagePreview.layer; NSLog(@"viewLayer = %@", viewLayer); AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; captureVideoPreviewLayer.frame = self.vImagePreview.bounds; [self.vImagePreview.layer addSublayer:captureVideoPreviewLayer]; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error = nil; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; if (!input) {
Create a new @property to save the link to the output object:
@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
Then do a UIImageView where it is good to display the captured photo. Add this to your NIB or programmatically.
Connect it to another @property or assign it manually, for example.
@property(nonatomic, retain) IBOutlet UIImageView *vImage;
Finally, create a UIButton so you can take a picture.
Add it again to your NIB (or programmatically to your screen) and connect it to the following method:
-(IBAction)captureNow { AVCaptureConnection *videoConnection = nil; for (AVCaptureConnection *connection in stillImageOutput.connections) { for (AVCaptureInputPort *port in [connection inputPorts]) { if ([[port mediaType] isEqual:AVMediaTypeVideo] ) { videoConnection = connection; break; } } if (videoConnection) { break; } } NSLog(@"about to request a capture from: %@", stillImageOutput); [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) { CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL); if (exifAttachments) {
You may need to import #import <ImageIO/CGImageProperties.h> .
Source Also check this one out .