【问题标题】:Camera in iOS app TutorialiOS 应用中的相机教程
【发布时间】:2013-12-19 23:28:35
【问题描述】:

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

非常感谢!

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

【问题讨论】:

标签: ios camera


【解决方案1】:

我不知道一个简单的教程,但是添加一个显示相机所见内容的视图非常容易。

第一:

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

第二:

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

#import <AVFoundation/AVFoundation.h>

第三:

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

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 等其他内容并添加相应的捕获委托.但在这种情况下,您应该遵循正确的教程或阅读文档。

【讨论】:

  • 如何使用这个自定义相机拍照?
  • 您使用具有此签名的方法:- (void)captureStillImageAsynchronouslyFromConnection:(AVCaptureConnection *)connection completionHandler:(void (^)(CMSampleBufferRef imageDataSampleBuffer, NSError *error))handler;
猜你喜欢
  • 2011-10-03
  • 2012-08-29
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
相关资源
最近更新 更多