我想知道是否有人愿意分享如何在iOS应用程序中添加摄像头功能,或者是否有人知道一个简单的教程。没有一个有任何按钮,只是在屏幕上显示什么是相机看到的。我试过了苹果的文档,但是它太复杂了,无法满足我的需要。
非常感谢!
编辑:任何简单的教程都可以。就像我说的,我不需要任何其他的东西,除了它来显示相机看到的东西。
发布于 2013-12-20 00:37:15
我不知道一个简单的教程,但添加一个视图,显示什么相机看到的是超级容易。
First:
添加一个UIView到您的界面构建器,将在那里的相机将显示。
第二版:
将AVFoundation框架添加到项目中,并将其导入到ViewController .m文件中。
#import <AVFoundation/AVFoundation.h>第三版:
将这两个变量添加到接口变量声明中
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];备注:
https://stackoverflow.com/questions/20693699
复制相似问题