ios - iOS 应用教程中的相机

标签 ios camera

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

非常感谢!

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

最佳答案

我不知道有没有简单的教程,但添加一个显示相机所见内容的 View 非常简单。

首先:

将 UIView 添加到您的界面构建器中,这将是相机显示的位置。

第二:

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

#import <AVFoundation/AVFoundation.h>

第三:

将这 2 个变量添加到您的接口(interface)变量声明中

AVCaptureVideoPreviewLayer *_previewLayer;
AVCaptureSession *_captureSession;

第四:

将此代码添加到您的 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];

注意事项:

  1. 断言将使程序在相机不可用的地方退出。

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

关于ios - iOS 应用教程中的相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693699/

相关文章:

Android - 扭曲的相机预览

java - LibGDX/Android开发: Managing cameras across multiple screens

python - 相机校准基本疑点

java - Android 应用程序中颜色名称的 RGB 值

ios - Google Places for iOS SDK v3.2.0 抛出错误

ios - 如何在 Swift 中将 CGAffineTransform 作为函数参数传递?

ios - 如何在创建对象时快速自定义 Parse 中的 ObjectId

android - 如何将对 "video/photo"附加的支持从 whatsapp 等应用程序添加到我的应用程序?

ios - Apple Search Ads + Localytics

ios - 正则表达式仅用于UITextfield中的名称