设置背景图像/视图到实时相机视图?

时间:2021-05-31 22:43:48

Is it possible to set a background for a particular view controller to show a live camera view? If so, could one lead me in the right direction to make this possible?

是否可以为特定的视图控制器设置背景以显示实时的摄像机视图?如果是这样的话,能不能引导我朝正确的方向去做呢?

3 个解决方案

#1


9  

Yes, definitely possible. You can embed live camera feed in UIView, which you can place anywhere you like.

是的,绝对有可能的。你可以在UIView中嵌入实时的相机feed,你可以把它放在任何你喜欢的地方。

Start by reading here: AVFoundation Reference - this is your framework

从这里开始读:AVFoundation Reference -这是你的框架

Particular class that you are looking for is AVCaptureVideoPreviewLayer

您正在寻找的特定类是AVCaptureVideoPreviewLayer

Which works in unison with AVCaptureSession

哪些与AVCaptureSession一致

And this is an example project that covers everything you need: AVCam

这是一个例子项目,涵盖了所有你需要的东西:AVCam

#2


7  

Import:

进口:

#import <AVFoundation/AVFoundation.h>

To add camera view to a controller's view add this code in the viewDidLoad:

要向控制器视图中添加摄像头视图,请在viewDidLoad中添加以下代码:

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
[session addInput:input];

AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
newCaptureVideoPreviewLayer.frame = self.view.bounds;

[self.view.layer addSublayer:newCaptureVideoPreviewLayer];

[session startRunning];

#3


3  

I think your best bet is to grab and understand this apple sample code, called AVCam. You'll see in the code how to create an AVCaptureVideoPreviewLayer. You'll insert this as a sublayer of a UIView that you'll use as your "background".

我认为最好的办法是获取并理解这个名为AVCam的苹果示例代码。您将在代码中看到如何创建AVCaptureVideoPreviewLayer。您将把它作为一个UIView的sublayer插入,您将使用它作为您的“背景”。

Once you've got that working, that UIView will be just like any other part of your view hierarchy. You can treat it like a background UIImageView (albeit, one that consumes a lot more batter power).

一旦你让它工作了,UIView就会和你的视图层次结构的其他部分一样。你可以把它当作一个背景UIImageView(虽然,它消耗了更多的打击力量)。

#1


9  

Yes, definitely possible. You can embed live camera feed in UIView, which you can place anywhere you like.

是的,绝对有可能的。你可以在UIView中嵌入实时的相机feed,你可以把它放在任何你喜欢的地方。

Start by reading here: AVFoundation Reference - this is your framework

从这里开始读:AVFoundation Reference -这是你的框架

Particular class that you are looking for is AVCaptureVideoPreviewLayer

您正在寻找的特定类是AVCaptureVideoPreviewLayer

Which works in unison with AVCaptureSession

哪些与AVCaptureSession一致

And this is an example project that covers everything you need: AVCam

这是一个例子项目,涵盖了所有你需要的东西:AVCam

#2


7  

Import:

进口:

#import <AVFoundation/AVFoundation.h>

To add camera view to a controller's view add this code in the viewDidLoad:

要向控制器视图中添加摄像头视图,请在viewDidLoad中添加以下代码:

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
[session addInput:input];

AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
newCaptureVideoPreviewLayer.frame = self.view.bounds;

[self.view.layer addSublayer:newCaptureVideoPreviewLayer];

[session startRunning];

#3


3  

I think your best bet is to grab and understand this apple sample code, called AVCam. You'll see in the code how to create an AVCaptureVideoPreviewLayer. You'll insert this as a sublayer of a UIView that you'll use as your "background".

我认为最好的办法是获取并理解这个名为AVCam的苹果示例代码。您将在代码中看到如何创建AVCaptureVideoPreviewLayer。您将把它作为一个UIView的sublayer插入,您将使用它作为您的“背景”。

Once you've got that working, that UIView will be just like any other part of your view hierarchy. You can treat it like a background UIImageView (albeit, one that consumes a lot more batter power).

一旦你让它工作了,UIView就会和你的视图层次结构的其他部分一样。你可以把它当作一个背景UIImageView(虽然,它消耗了更多的打击力量)。