0

I captured image using below code

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];

[session startRunning];

_stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[_stillImageOutput setOutputSettings:outputSettings];

[session addOutput:_stillImageOutput];

when i press the button

   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;
     _vImage.hidden=YES;
     UIStoryboard *storybord=[UIStoryboard storyboardWithName:@"Main" bundle:nil];

     shareViewController   *shareview=[storybord instantiateViewControllerWithIdentifier:@"share"];
     [self presentViewController:shareview animated:YES completion:nil];

     shareview.shareimageview.image=image;

     NSMutableArray *temparray = [NSMutableArray arrayWithObjects:image,nil];
    NSMutableArray  *newparsetile=[@[@"you"]mutableCopy];
     shareview.newtile=newparsetile;
     shareview.selectedimgarray=temparray;


     [[NSNotificationCenter defaultCenter] postNotificationName:@"Shareimage" object:image];


 }];

how to save the output image in to the device document directory,can any body help me out,answer with code is appreciated,since i am new to the ios objective c,the people who want to customize the camera like instagram can use my code it is 100% working

2 Answers2

1
NSData *pngData = UIImagePNGRepresentation(image);
NSArray *paths =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"image_name”]]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
Palanichamy
  • 106
  • 5
0
// Saving it to documents direcctory
     NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

     NSString *documentDirectory = [directoryPaths objectAtIndex:0];
     NSString* filePath = [documentDirectory stringByAppendingPathComponent:@"FileName.png"];
     NSData *imageData = // Some Image data;
     NSURL *url = [NSURL fileURLWithPath:filePath];

     if ([imageData writeToURL:url atomically:YES]) {
         NSLog(@"Success");
     }
     else{
         NSLog(@"Error");
     }

You can use above code to save an image to documents directory. Instead of imagedata variable you can pass your variable.

ManiaChamp
  • 811
  • 7
  • 20
  • Thanks for the quick responce but we are redefining these NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; – Satheeshkumar Naidu Aug 03 '16 at 08:23
  • @SatheeshkumarNaidu: Ok , You can simply put the writing code where ever you want to write an image to documents directory as per your need.I am editing the answer if its resolve your problem . Mark it as answer in order to help others. – ManiaChamp Aug 03 '16 at 08:29
  • it is not getting saved.in tis place what should i give – Satheeshkumar Naidu Aug 03 '16 at 08:29
  • If you are getting the NSData of your image then you just need to write using the above code. Instead of "imageData" variable put your variable. – ManiaChamp Aug 03 '16 at 08:34
  • captured image is not showing in the document directory – Satheeshkumar Naidu Aug 03 '16 at 08:36
  • Debug the filepath and try to look at the same path and also check if it goes in to sucess or error . If error check whether your Data is nil or not. – ManiaChamp Aug 03 '16 at 08:40
  • NSData *imageData = // Some Image data; some image data means NSURL *url = [NSURL fileURLWithPath:filePath];,it is showing error as unexpected use of interface name nsurl expected expression , use of undeclared identifier url – Satheeshkumar Naidu Aug 03 '16 at 08:49
  • No , NSURL *url = [NSURL fileURLWithPath:filePath]; , It doesn't means that. ImageData means NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; – ManiaChamp Aug 03 '16 at 09:49