在一些運用中,我們須要用到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 ;
}
}
}
下面的代碼是應用前置攝像頭和後置攝像頭!
【iOS開辟中關於攝像頭的一些根本應用辦法分享】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!