我想知道是否有人愿意分享如何将相机功能放入iOS应用程序,或者是否有人知道一个简单的教程。没有任何按钮,只是在屏幕上显示相机所看到的内容。我试过苹果的文档,但对我的需求来说太复杂了。
非常感谢!
编辑:任何简单的教程都可以。就像我说的,我不需要任何其他东西,除了它来显示相机看到的东西。
我不知道有什么简单的教程,但添加一个显示相机所见内容的视图非常容易。
第一个:
将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];
注意:
断言将使程序在没有摄像头的地方退出。
这只显示相机所见内容的"预览",如果您想操作输入、拍照或录制视频,则需要配置其他内容,如会话预设并添加相应的捕获代理。但在这种情况下,您应该遵循适当的教程或阅读文档。