Iphone UIImageView中的实时摄影机

Iphone UIImageView中的实时摄影机,iphone,ios,xcode,ios6,Iphone,Ios,Xcode,Ios6,有人知道我如何将实时摄像机输入UIImageView吗 我有一个自定义用户界面,需要在其中显示摄像头馈送前置摄像头,因此我无法使用UIImagePickerControl。您需要创建一个捕获会话,并启动它运行。完成后,您可以将捕获会话中的图层添加到视图中: - (void)setupCaptureSession { NSError* error = nil; // Create the session _captureSession = [[AVCaptureSessi

有人知道我如何将实时摄像机输入UIImageView吗


我有一个自定义用户界面,需要在其中显示摄像头馈送前置摄像头,因此我无法使用UIImagePickerControl。

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

- (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]; 
}
_myView.layer addSubLayer:_previewLayer];
完成此操作后,可以按如下方式创建预览层:

_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
[_captureSession startRunning];
然后将预览层添加到视图中:

- (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]; 
}
_myView.layer addSubLayer:_previewLayer];

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

- (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]; 
}
_myView.layer addSubLayer:_previewLayer];
完成此操作后,可以按如下方式创建预览层:

_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
[_captureSession startRunning];
然后将预览层添加到视图中:

- (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]; 
}
_myView.layer addSubLayer:_previewLayer];

查看AVCaptureVideoPreviewLayer类查看AVCaptureVideoPreviewLayer类您好,感谢您的快速回复!我已经试过了,但是UIImageView仍然没有显示在相机上。我已将_myView替换为UIImageView现在要做什么?_myView是要将相机添加到的视图的名称。您可以尝试使用以下选项设置该视图:_camera.previewLayer.frame=_myView.bounds;运行[\u captureSession startRunning]时,它为我提供了以下视图。。。但我有个例外。。有什么好主意吗?让我们来打招呼,谢谢你的快速回复!我已经试过了,但是UIImageView仍然没有显示在相机上。我已将_myView替换为UIImageView现在要做什么?_myView是要将相机添加到的视图的名称。您可以尝试使用以下选项设置该视图:_camera.previewLayer.frame=_myView.bounds;运行[\u captureSession startRunning]时,它为我提供了以下视图。。。但我有个例外。。有什么想法吗?让我们