2015-10-19 98 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