简体   繁体   English

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

[英]How to save captured image in to the document directory

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 如何将输出图像保存到设备文档目录中,任何机构都可以帮助我,感谢提供代码答复,因为我是ios目标c的新手,所以想要自定义instagram之类的相机的人可以使用我的代码这是100%的工作

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
// 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. 您可以传递变量来代替imagedata变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cordova-plugin-file 在 ios 上不起作用,无法将我捕获的图像保存在 Document 目录中 - cordova-plugin-file is not working on ios and unable to save my captured image in Document directory 如何将图像保存在文档目录中并在 Swift 中获取它 - How to save image in document directory and fetch it in Swift 如何在文档目录中保存唯一的图像名称 - How to save unique image name in document directory 如何使用 writeToFile 将图像保存在文档目录中? - how to use writeToFile to save image in document directory? 如何将捕获的图像保存到代号为Gallery的库中? - How to Save Captured Image to Gallery codename one? 如何将 UIPickerViewController 中的图像或视频保存到文档目录? - How to save image or video from UIPickerViewController to document directory? 如何保存使用Camera捕获的图像并将其保存在另一个ViewController中? - How to save the image captured using Camera and save in another ViewController? 使用QRCode将图像保存在后台线程的文档目录中 - save image with QRCode in document directory in background thread 如何将图像保存在从iPhone中的UIGraphicsGetImageFromCurrentImageContext()捕获的系统中? - how to save the image in system which is captured from UIGraphicsGetImageFromCurrentImageContext() in iPhone? 如何在图像上应用Perspective 3d Transform并将该图像保存在文档目录中 - How to apply Perspective 3d Transform on image and save that image in document directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM