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

问题描述:

我使用以下代码如何保存捕获的图像中的文档目录

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,谁想要的人定制相机就像是100%的工作

的Instagram可以使用我的代码3210

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

+0

感谢您的快速性反应,但我们正在重新定义这些的NSData *为imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]。 –

+0

@SatheeshkumarNaidu:好吧,你可以简单地把任何你想要写的文件目录中的图像,按您的need.I我编辑的答案,如果它解决您的问题写代码。将其标记为答案以帮助其他人。 – ManiaChamp

+0

它是没有得到saved.in那朵地方我应该怎么给 –

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 
+0

我应该在哪里添加此代码 –

+0

在捕获/选择图像后的按钮单击事件。 – Palanichamy

+0

应用程序在NSString * filePath = [文件路径stringByAppendingPathComponent:[NSString stringWithFormat:@“image_name”]]]时崩溃;用日志推广日志中的崩溃消息 –