iphone - 在 UIView 中显示相机提要

标签 iphone ios objective-c

我正在研究将前置摄像头视频输入显示到 UIView 中,类似于 FaceTime 的做法。我知道这可以使用 AVCaptureVideoPreviewLayer 轻松完成。有没有不使用 AVCaptureVideoPreviewLayer 的另一种方法?

这仅用于教育目的。

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

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

最佳答案

如果您正在尝试操作像素,您可以将以下方法放在您作为委托(delegate)分配给 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 Controller 中的 UIImageView。 查看 kCGImageAlphaNone 标志。这将取决于您在做什么。

关于iphone - 在 UIView 中显示相机提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14902781/

相关文章:

ios - 如何通过单击 UITableViewCell 按钮将数据发送到另一个 ViewController

ios - 使用 C 数组的对象的深拷贝

iphone - 如何让 Xcode 刷新项目图像文件的本地副本?

ios - 无限循环试图从 IB 中的 xib 创建自定义 View

ios - 在服务器通知后更新应用程序中的数据 - 即使强制退出?

ios - 如何在 Xcode 6.0 Beta 中运行 iOS 7.1 模拟器?

iphone - 当通过 UIWebView 播放电影,然后通过 Airplay 输出到 AppleTV 时,关闭 iPhone 屏幕会停止播放。为什么?

ios - 启动屏幕后应用程序崩溃

iphone - UIControlStateHighlighted 和 UIControlStateSelected 有什么区别?

iphone - 在 iPad 设备上测试时如何访问内存泄漏工具?