iphone - UIImageView 中的实时摄像头

标签 iphone ios xcode ios6

有谁知道我如何将实时摄像头输入 UIImageView ?

我有一个自定义 UI,我需要在其中显示摄像头馈送(前置摄像头),因此我无法使用 UIImagePickerControl .

最佳答案

您需要创建一个捕获 session ,并启动它运行。完成后,您可以将捕获 session 中的图层添加到您的 View 中:

- (void)setupCaptureSession
{
    NSError* error = nil;

    // Create the session
    _captureSession = [[AVCaptureSession alloc] init];    
    _captureSession.sessionPreset = AVCaptureSessionPresetMedium;
    AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

    [_captureSession addInput:input];
    AVCaptureVideoDataOutput* output = [[AVCaptureVideoDataOutput alloc] init];
    [_captureSession addOutput:output];

    // Configure your output.
   dispatch_queue_t queue = dispatch_queue_create("myCameraOutputQueue", NULL);
   //If you want to sebsequently use the data, then implement the delegate.
   [output setSampleBufferDelegate:self queue:queue]; 
}

完成后,您可以创建一个预览层,如下所示:
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
[_captureSession startRunning];

然后将预览层添加到您的 View 中:
_myView.layer addSubLayer:_previewLayer];

关于iphone - UIImageView 中的实时摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14337489/

相关文章:

iphone - 如何在 iphone 中使用蜂窝塔三角测量?

ios - 更改 iPhone 应用程序中的系统弹出窗口以获取核心位置权限

ios - 为什么这些约束在动画时似乎滞后?

ios - 将 sqlite 文件复制到 Documents 文件夹后,文件变空

xcode - block 缩进 Xcode

iPhone/iPad SimpleTableViewCells : What replaces the deprecated setImage setText on table cells?

iphone - CGBitmapContextCreate的处理错误

ios - 如何同步2个不同层的动画

xcode - 编译时间极其缓慢

ios - SwiftUI 导航栏和状态栏 - 让它们颜色相同