服务器之家

服务器之家 > 正文

iOS开发中对于摄像头的一些基本使用方法分享

时间:2020-12-25 15:45     来源/作者:iOS开发网

在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频。并且从相册中选取我们需要的图片或者视频。
关于iOS摄像头和相册的应用,可以使用UIImagePickerController类来完成控制。

UIImagePickerController 这个类可以为大家提供照相的功能,以及图片,视频浏览的功能。


检查硬件是否安装有摄像头或者允许操作相册

 

复制代码 代码如下:


#pragma mark - 摄像头和相册相关的公共类

 


// 判断设备是否有摄像头
- (BOOL) isCameraAvailable{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}


// 前面的摄像头是否可用
- (BOOL) isFrontCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}


// 后面的摄像头是否可用
- (BOOL) isRearCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}

 

 

 

调用摄像头

复制代码 代码如下:

- (BOOL) hasMultipleCameras {
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 ;
}
}
}

 


上面的代码是使用前置摄像头和后置摄像头!

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
返回顶部