2013-02-15 42 views
0

我正在寻找将前端摄像头视频源显示到类似于FaceTime的UIView中。我知道这可以很容易地使用AVCaptureVideoPreviewLayer完成。是否有另一种方法可以在不使用AVCaptureVideoPreviewLayer的情况下执行此操作?在UIView中显示摄像头供稿

这仅仅是为了教育目的。

更新: 我发现,这可以用的UIImagePickerController完成

UIImagePickerController *cameraView = [[UIImagePickerController alloc] init]; 
cameraView.sourceType = UIImagePickerControllerSourceTypeCamera; 
cameraView.showsCameraControls = NO; 
[self.view addSubview:cameraView.view]; 
[cameraView viewWillAppear:YES]; 
[cameraView viewDidAppear:YES]; 
+1

为什么你不想使用AVCaptureVideoPreviewLayer? – 2013-02-15 20:32:32

+0

我只是想评估所有其他选项。 – ijason03 2013-02-15 20:48:12

回答

3

如果你试图操纵像素,你可以把你要AVCaptureVideoDataOutputSampleBufferDelegate分配为代表的类下面的方法:

-(void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection 
{ 
    CVImageBufferRef pb = CMSampleBufferGetImageBuffer(sampleBuffer); 

    if(CVPixelBufferLockBaseAddress(pb, 0)) //zero is success 
    NSLog(@"Error"); 

    size_t bufferHeight = CVPixelBufferGetHeight(pb); 
    size_t bufferWidth = CVPixelBufferGetWidth(pb); 
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pb); 

    unsigned char* rowBase= CVPixelBufferGetBaseAddress(pb); 


    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB(); 
    if (colorSpace == NULL) 
     NSLog(@"Error"); 

    // Create a bitmap graphics context with the sample buffer data. 
    CGContextRef context= CGBitmapContextCreate(rowBase,bufferWidth,bufferHeight, 8,bytesPerRow, colorSpace, kCGImageAlphaNone); 

    // Create a Quartz image from the pixel data in the bitmap graphics context 
    CGImageRef quartzImage = CGBitmapContextCreateImage(context); 

    UIImage *currentImage=[UIImage imageWithCGImage:quartzImage]; 

    // Free up the context and color space 
    CFRelease(quartzImage); 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 

    if(CVPixelBufferUnlockBaseAddress(pb, 0)) //zero is success 
    NSLog(@"Error"); 
} 

然后将该图像连接到View控制器中的UIImageView。 Lookinto kCGImageAlphaNone标志。这将取决于你在做什么。

+0

不想操纵像素。只需寻找替代解决方案。 – ijason03 2013-02-15 21:20:38

+0

好极了。如果这有效,请不要忘记标记为答案。谢谢 – Spectravideo328 2013-02-15 21:22:44