首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS应用教程中的摄像头

iOS应用教程中的摄像头
EN

Stack Overflow用户
提问于 2013-12-19 23:28:35
回答 1查看 7.9K关注 0票数 8

我想知道是否有人愿意分享如何在iOS应用程序中添加摄像头功能,或者是否有人知道一个简单的教程。没有一个有任何按钮,只是在屏幕上显示什么是相机看到的。我试过了苹果的文档,但是它太复杂了,无法满足我的需要。

非常感谢!

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-20 00:37:15

我不知道一个简单的教程,但添加一个视图,显示什么相机看到的是超级容易。

First:

添加一个UIView到您的界面构建器,将在那里的相机将显示。

第二版:

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

代码语言:javascript
复制
#import <AVFoundation/AVFoundation.h>

第三版:

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

代码语言:javascript
复制
AVCaptureVideoPreviewLayer *_previewLayer;
AVCaptureSession *_captureSession;

第四:

将此代码添加到您的viewDidLoad中。(对其所做的解释作了评论)

代码语言:javascript
复制
//-- 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];

备注:

  1. 断言将使程序退出在没有相机的地方。
  2. 这只显示相机看到的内容的“预览”,如果您想要操作输入,或者拍摄一张照片或录制视频,您需要配置额外的东西,比如SessionPreset,并添加相应的捕获委托。但在这种情况下,您应该遵循适当的教程或阅读文档。
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20693699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档