iOS应用程序教程中的相机

Bra*_*nch 8 camera ios

我想知道是否有人愿意分享如何将相机功能放入iOS应用程序,或者如果有人知道一个简单的教程.没有任何按钮,只是在屏幕上显示相机看到的内容.我尝试过Apple的文档,但它对我的需求来说太复杂了.

非常感谢!

编辑:任何简单的教程都可以.就像我说的,我不需要任何其他东西,除了它以显示相机看到的东西.

Poc*_*chi 15

我不知道一个简单的教程,但添加一个显示相机看到的视图是非常容易的.

第一:

将UIView添加到界面构建器中,该构建器将显示摄像机.

第二:

将AVFoundation框架添加到项目中,并将其导入添加到ViewController .m文件中.

#import <AVFoundation/AVFoundation.h>
Run Code Online (Sandbox Code Playgroud)

第三:

将这两个变量添加到接口变量声明中

AVCaptureVideoPreviewLayer *_previewLayer;
AVCaptureSession *_captureSession;
Run Code Online (Sandbox Code Playgroud)

第四:

将此代码添加到viewDidLoad.(对它的作用的解释被评论)

//-- Setup Capture Session.
_captureSession = [[AVCaptureSession alloc] init];

//-- Creata a video device and input from that Device.  Add the input to the capture session.
AVCaptureDevice * videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if(videoDevice == nil)
    assert(0);

//-- Add the device to the session.
NSError *error;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice
                                                                    error:&error];
if(error)
    assert(0);

[_captureSession addInput:input];

//-- Configure the preview layer
_previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
_previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[_previewLayer setFrame:CGRectMake(0, 0,
                                   self.cameraPreviewView.frame.size.width,
                                   self.cameraPreviewView.frame.size.height)];

//-- Add the layer to the view that should display the camera input
[self.cameraPreviewView.layer addSublayer:_previewLayer];

//-- Start the camera
[_captureSession startRunning];
Run Code Online (Sandbox Code Playgroud)

笔记:

  1. 断言将使程序退出相机不可用的位置.

  2. 这仅显示相机所看到的内容的"预览",如果您想要操作输入,或拍照或录制您需要配置其他内容(如SessionPreset)并添加相应的捕获代表所需的视频.但在这种情况下,您应该遵循适当的教程或阅读文档.