How to save photos taken with AVFoundation in a photo album?

AVFoundation is just great for those who want to get their hands dirty, but there are many more basic things that are not easy to understand how to save photos taken from your device into a photo album

Any ideas?

+11
ios objective-c iphone cocoa avfoundation
Oct 28
source share
4 answers

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) { // Handle the error appropriately. NSLog(@"ERROR: trying to open camera: %@", error); } [session addInput:input]; stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; [stillImageOutput setOutputSettings:outputSettings]; [session addOutput:stillImageOutput]; [session startRunning]; } 

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) { // Do something with the attachments. NSLog(@"attachements: %@", exifAttachments); } else { NSLog(@"no attachments"); } NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; UIImage *image = [[UIImage alloc] initWithData:imageData]; self.vImage.image = image; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); }]; } 

You may need to import #import <ImageIO/CGImageProperties.h> .

Source Also check this one out .

+62
Nov 03
source share

According to your question, it looks like you already have a camera image in NSData or UIImage. If so, you can add this image to the album in many ways. AVFoundation itself does not have classes that carry images. So, for example, you can use the ALAssetsLibrary frame to save the image in a photo album. Or you can only use the UIKit interface with it the UIImageWriteToSavedPhotosAlbum method . Both are good to use.

If you don't already have a snapshot, you can view the captureStillImageAsynchronouslyFromConnection method of the AVFoundation structure. Anyway, here are the ideas. You can easily find examples on the Internet. Good luck :)

+1
Nov 02
source share

There are many ways to configure the camera so that you either do not perform or do not show.

Best place to search: AVCamCaptureManager.m in the AVCamCaptureManager.m sample project; especially setupSession and captureStillImage (which writes a photo to the library).

0
Oct 28 '12 at 5:24
source share

This method works for me.

 -(void) saveImageDataToLibrary:(UIImage *) image { NSData *imageData = UIImageJPEGRepresentation(image, 1.0); [appDelegate.library writeImageDataToSavedPhotosAlbum:imageData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { if (error) { NSLog(@"%@",error.description); } else { NSLog(@"Photo saved successful"); } }]; } 

where appDelegate.library is an ALAssetLibrary instance.

0
Nov 02
source share



All Articles