2015-10-19 96 views
0

我正在構建一個應用程序,我想從攝像頭拍攝快照並將其顯示在UIImageView中。我可以拍攝快照,但AVCaptureVideoPreviewLayer在屏幕截圖中不可見。有誰知道這是怎麼做到的嗎? 這裏是我的代碼:ios objective C截圖底層不可見

@implementation ViewController 
CGRect imgRect; 
AVCaptureVideoPreviewLayer *previewLayer; 
AVCaptureVideoDataOutput *output; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    //Capture Session 
    AVCaptureSession *session = [[AVCaptureSession alloc]init]; 
    session.sessionPreset = AVCaptureSessionPresetPhoto; 

    //Add device 
    AVCaptureDevice *device = 
    [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

    //Input 
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; 

    if (!input) 
    { 
     NSLog(@"No Input"); 
    } 

    [session addInput:input]; 

    //Output 
    output = [[AVCaptureVideoDataOutput alloc] init]; 
    [session addOutput:output]; 
    output.videoSettings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) }; 

    //Preview 
    previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; 

    CGFloat x = self.view.bounds.size.width * 0.5 - 128; 
    imgRect = CGRectMake(x, 64, 256, 256); 
    previewLayer.frame = imgRect; 
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    [self.view.layer addSublayer:previewLayer]; 

    //Start capture session 
    [session startRunning]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)TakeSnapshot:(id)sender { 
    self.imgResult.image = self.pb_takeSnapshot; 
} 

- (UIImage *)pb_takeSnapshot { 
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale); 
    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]; 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 
@end 

幫助一點是非常讚賞。 預先感謝您

吉爾伯特Avezaat

回答

0

您應該使用AVCaptureStillImageOutput從相機連接獲取圖像,

這裏是你如何能做到這一點,

AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
    stillImageOutput.outputSettings = @{ 
             AVVideoCodecKey: AVVideoCodecJPEG, 
             (__bridge id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA) 
             }; 
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { 

     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; 
     UIImage *image = [UIImage imageWithData:imageData]; 
    }]; 
+0

好的,謝謝。我會着眼於此。 – MMWizard

+0

我試過你是例子,它實際上顯示圖像。但是圖像被沖掉了,請參閱http://www.firstpolygon.com/images/example01.jpg瞭解輸出。它也旋轉? – MMWizard

+0

@MMWizard,好吧,可能會有一些設置不匹配。我已經編輯了上面的答案。您也可以直接從CMSampleBufferRef生成圖像。 – Sandeep

0

首先檢查圖片返回與否。如果返回然後...

- (IBAction)TakeSnapshot:(id)sender { 

self.imgResult.image = self.pb_takeSnapshot; 

[self.view bringSubviewToFrunt:self.imgResult]; 

} 

希望它可以幫助你。

+0

喜加里,它仍然是相同的。它不會在屏幕截圖中顯示相機預覽。我實際需要的是來自預覽圖層中相機預覽的快照。 – MMWizard

+0

此方法pb_takeSnapshot返回圖像或不。 – Garry

+0

好吧,它返回一個圖像。但不包括相機預覽圖層。或者是否有可能從該圖層抓取一幀。因爲那是我需要的。 – MMWizard