2016-08-03 65 views
0

我使用以下代碼如何保存捕獲的圖像中的文檔目錄

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

回答

0
的Instagram可以使用我的代碼3210

您可以使用上述代碼將圖像保存到文檔目錄。而不是imagedata變量,你可以傳遞你的變量。

+0

感謝您的快速性反應,但我們正在重新定義這些的NSData *爲imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]。 –

+0

@SatheeshkumarNaidu:好吧,你可以簡單地把任何你想要寫的文件目錄中的圖像,按您的need.I我編輯的答案,如果它解決您的問題寫代碼。將其標記爲答案以幫助其他人。 – ManiaChamp

+0

它是沒有得到saved.in那朵地方我應該怎麼給 –

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

我應該在哪裏添加此代碼 –

+0

在捕獲/選擇圖像後的按鈕單擊事件。 – Palanichamy

+0

應用程序在NSString * filePath = [文件路徑stringByAppendingPathComponent:[NSString stringWithFormat:@「image_name」]]]時崩潰;用日誌推廣日誌中的崩潰消息 –

相關問題