【问题标题】:How to save captured image in to the document directory如何将捕获的图像保存到文档目录
【发布时间】:2016-08-03 06:46:52
【问题描述】:

我使用以下代码捕获图像

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% 工作的

【问题讨论】:

    标签: ios objective-c xcode avcapturesession alassetslibrary


    【解决方案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
    

    【讨论】:

    • 在捕获/选择图像后的按钮点击事件中。
    • 应用程序在 NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"image_name"]]; 处崩溃,而在日志中没有提示崩溃消息
    • 替换成这个 NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image_name"];
    • @"image_name" 我应该添加什么
    • 只是一个文件名。你可以给任何字符串。
    【解决方案2】:
    // 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");
         }
    

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

    【讨论】:

    • 感谢您的快速响应,但我们正在重新定义这些 NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
    • @SatheeshkumarNaidu:好的,您可以根据需要简单地将编写代码放在您想要将图像写入文档目录的任何位置。如果它解决了您的问题,我正在编辑答案。将其标记为答案以帮助他人。
    • 它没有被保存。在这个地方我应该给什么
    • 如果您正在获取图像的 NSData,那么您只需要使用上面的代码进行编写。而不是“imageData”变量放你的变量。
    • 截取的图像没有显示在文档目录中
    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    相关资源
    最近更新 更多