这篇文章主要介绍了iOS开发中对于摄像头的一些基本使用方法分享,包括判断摄像头是否可用的方法,需要的朋友可以参考下
在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频。并且从相册中选取我们需要的图片或者视频。
关于iOS摄像头和相册的应用,可以使用UIImagePickerController类来完成控制。
UIImagePickerController 这个类可以为大家提供照相的功能,以及图片,视频浏览的功能。
检查硬件是否安装有摄像头或者允许操作相册
#pragma mark - 摄像头和相册相关的公共类
// 判断设备是否有摄像头
- (BOOL) isCameraAvailable{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
// 前面的摄像头是否可用
- (BOOL) isFrontCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}
// 后面的摄像头是否可用
- (BOOL) isRearCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}
调用摄像头
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
if (devices != nil && [devices count] > 1) return YES;
return NO;
}
- (AVCaptureDevice *)cameraWithPosition : (AVCaptureDevicePosition) position
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices )
if ( device.position == position )
return device;
return nil ;
}
- (void) swapFrontAndBackCameras {
//check for available cameras!
if (![self hasMultipleCameras]) return;
//assumes session is running
NSArray *inputs = self.captureSession.inputs; //should only be one value!
for ( AVCaptureDeviceInput *captureDeviceInput in inputs ) {
AVCaptureDevice *device = captureDeviceInput.device ;
if ( [device hasMediaType:AVMediaTypeVideo ] ) {
AVCaptureDevicePosition position = device.position ;
AVCaptureDevice *newCamera = nil ;
AVCaptureDeviceInput *newInput = nil ;
if (position == AVCaptureDevicePositionFront)
newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
else
newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
[self initializeCaptureDevice:newCamera];
newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil];
// beginConfiguration ensures that pending changes are not applied immediately
[self.captureSession beginConfiguration ];
[self.captureSession removeInput:captureDeviceInput]; //remove current
[self.captureSession addInput:newInput]; //add new
// Changes take effect once the outermost commitConfiguration is invoked.
[self.captureSession commitConfiguration];
break ;
}
}
}
上面的代码是使用前置摄像头和后置摄像头!
本文标题为:iOS开发中对于摄像头的一些基本使用方法分享
基础教程推荐
- Android Compose自定义TextField实现自定义的输入框 2023-05-13
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18
- Android开发Compose集成高德地图实例 2023-06-15
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- iOS开发使用XML解析网络数据 2022-11-12
- Android实现短信验证码输入框 2023-04-29
- IOS获取系统相册中照片的示例代码 2023-01-03
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07
- iOS开发 全机型适配解决方法 2023-01-14
- Flutter进阶之实现动画效果(三) 2022-10-28