如何将捕获的图像保存到文档目录中

时间:2016-08-03 06:46:52

标签: ios objective-c xcode avcapturesession alassetslibrary

我使用以下代码

捕获图像
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];

当我按下按钮

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


 }];

如何将输出图像保存到设备文档目录中,任何人体都可以帮助我,对代码的回答表示赞赏,因为我是ios目标c的新手,想要像Instagram一样自定义相机的人可以使用我的代码它是100%工作

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

JSON.pretty_generate(@users.to_json)

您可以使用上面的代码将图像保存到文档目录。您可以传递变量而不是imagedata变量。