介绍
小编给大家分享一下iOS实现摄像头实时采集图像的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!
iOS实现摄像头实时采集图像的具体内容如下
新接到一个实时获取摄像头当前照片的需求,在设定的时间内需要保持摄像头处在开启状态并可以实时回调到当前的图片数据信息;
此次结合AVCaptureDevice, AVCaptureSession, AVCaptureVideoPreviewLayer将其与UIView, UIImageView和界面图像相结合;
Github
具体实现代码如下:
# import & lt; UIKit/UIKit.h> # import & lt; CoreVideo/CoreVideo.h> # import & lt; CoreMedia/CoreMedia.h> # import & lt; AVFoundation/AVFoundation.h> , NS_ASSUME_NONNULL_BEGIN , @interface YHCameraView : UIView & lt; AVCaptureVideoDataOutputSampleBufferDelegate> , @property (原子,弱),UIImageView * cameraImageView; @property (强,,原子),AVCaptureDevice *,设备; @property (强,,原子),AVCaptureSession *, captureSession; @property (强,,原子),AVCaptureVideoPreviewLayer *, previewLayer; @property (强,,原子),用户界面图像*,cameraImage; , @end , NS_ASSUME_NONNULL_END
# import “YHCameraView.h" , @implementation YHCameraView , 安康;(instancetype) initWithFrame:(CGRect中)frame { ,,,if (=self [super initWithFrame:框架]),{ ,,,,,,,self.backgroundColor =, [UIColor lightGrayColor]; ,,,,,,,(self createUI); ,,,} ,,,return 自我; } ,/*//,Only override 绘制矩形:,if 你perform custom 绘画。//,An empty implementation  adversely affects performance during 动画。 安康;(空白)绘制矩形(CGRect中):rect { ,,,//管理代码 } */, 安康;(空白)createUI { ,,,NSArray *, devices =, [AVCaptureDevice devicesWithMediaType AVMediaTypeVideo):; ,,,(AVCaptureDevice * device 拷贝设备) ,,,{ ,,,,,,,如果([device 位置],==,AVCaptureDevicePositionFront),//,前置摄像头 ,,,,,,,,,,,self.device =,设备; ,,,} ,,,, ,,,AVCaptureDeviceInput *, input =, (AVCaptureDeviceInput deviceInputWithDevice: self.device 错误:nil); ,,,AVCaptureVideoDataOutput *, output =, [[AVCaptureVideoDataOutput alloc], init); ,,,output.alwaysDiscardsLateVideoFrames =,是的, , ,,,dispatch_queue_t 队列; ,,,queue =, dispatch_queue_create (“cameraQueue",, NULL); ,,,(output setSampleBufferDelegate: self 队列:队列); , ,,,NSString *, key =, (NSString *), kCVPixelBufferPixelFormatTypeKey; ,,,NSNumber *, value =, [NSNumber numberWithUnsignedInt kCVPixelFormatType_32BGRA):; ,,,NSDictionary *, videoSettings =, (NSDictionary dictionaryWithObject: value forKey:例子); ,,,(output setVideoSettings videoSettings):; , ,,,self.captureSession =, [[AVCaptureSession alloc], init); ,,,(self.captureSession addInput:输入); ,,,(self.captureSession addOutput:输出); ,,,(self.captureSession setSessionPreset AVCaptureSessionPresetPhoto):; , ,,,self.previewLayer =, [AVCaptureVideoPreviewLayer layerWithSession self.captureSession):; ,,,self.previewLayer.videoGravity =, AVLayerVideoGravityResizeAspectFill; , ,,,//,CHECK FOR YOUR 应用 ,,,NSInteger screenWidth =, self.frame.size.width; ,,,NSInteger screenHeitht =, self.frame.size.height; ,,,self.previewLayer.frame =, self.bounds; ,,,self.previewLayer.orientation =, AVCaptureVideoOrientationPortrait; ,,,//,CHECK FOR YOUR 应用 ,//,,,,(self.layer insertSublayer: self.previewLayer atIndex: 0];,,,//, Comment-out 用hide preview 层 , ,,,(self.captureSession startRunning); } , 安康;(空白)captureOutput:(AVCaptureOutput *) captureOutput didOutputSampleBuffer: (CMSampleBufferRef) sampleBuffer fromConnection: (AVCaptureConnection *)连接 { null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nulliOS实现摄像头实时采集图像的方法