2013-02-15 110 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