AVCaptureDevice主要用来获取iPhone一些关于相机设备的相关属性。
必须引用一个AVFoundation框架:
//#import
1.前置和后置摄像头
typedef NS_ENUM(NSInteger, AVCaptureDevicePosition) {
AVCaptureDevicePositionUnspecified = 0,
AVCaptureDevicePositionBack = 1,
AVCaptureDevicePositionFront = 2
} NS_AVAILABLE(10_7, 4_0);
2.闪光灯开关
typedef NS_ENUM(NSInteger, AVCaptureFlashMode) {
AVCaptureFlashModeOff = 0,
AVCaptureFlashModeOn = 1,
AVCaptureFlashModeAuto = 2
} NS_AVAILABLE(10_7, 4_0);
3.手电筒开关--其实就是相机的闪光灯
typedef NS_ENUM(NSInteger, AVCaptureTorchMode) {
AVCaptureTorchModeOff = 0,
AVCaptureTorchModeOn = 1,
AVCaptureTorchModeAuto = 2,
} NS_AVAILABLE(10_7, 4_0);
4.焦距模式调整
typedef NS_ENUM(NSInteger, AVCaptureFocusMode) {
AVCaptureFocusModeLocked = 0,
AVCaptureFocusModeAutoFocus = 1,
AVCaptureFocusModeContinuousAutoFocus = 2,
} NS_AVAILABLE(10_7, 4_0);
5.曝光量调节
typedef NS_ENUM(NSInteger, AVCaptureExposureMode) {
AVCaptureExposureModeLocked = 0,
AVCaptureExposureModeAutoExpose = 1,
AVCaptureExposureModeContinuousAutoExposure = 2,
AVCaptureExposureModeCustom NS_ENUM_AVAILABLE_IOS(8_0) = 3,
} NS_AVAILABLE(10_7, 4_0);
6.白平衡
typedef NS_ENUM(NSInteger, AVCaptureWhiteBalanceMode) {
AVCaptureWhiteBalanceModeLocked = 0,
AVCaptureWhiteBalanceModeAutoWhiteBalance = 1,
AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance = 2,
} NS_AVAILABLE(10_7, 4_0);
7.距离调整
typedef NS_ENUM(NSInteger, AVCaptureAutoFocusRangeRestriction) {
AVCaptureAutoFocusRangeRestrictionNone = 0,
AVCaptureAutoFocusRangeRestrictionNear = 1,
AVCaptureAutoFocusRangeRestrictionFar = 2,
} NS_AVAILABLE_IOS(7_0);
下面简单实现以下手电筒的如何开启和关闭,为了简单起见,我们就借助touchesBegan方法操作,通过触摸屏幕开启和关闭手电筒
#import "ViewController.h"
#import
@interface ViewController ()
@property (nonatomic, strong) AVCaptureDevice *device;
@property (nonatomic,assign) BOOL lightOn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
/**
* hasTorch :返回YES表名手机上有手电筒
*/
if (![_device hasTorch]) {
NSLog(@"手电筒坏了");
return;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
_lightOn = !_lightOn;
//根据ligthOn状态判断打开还是关闭
if (_lightOn) {
//开启手电筒
[_device lockForConfiguration:nil];
[_device setTorchMode:AVCaptureTorchModeOn];
[_device unlockForConfiguration];
}else{
//关闭手电筒
[_device lockForConfiguration:nil];
[_device setTorchMode:AVCaptureTorchModeOff];
[_device unlockForConfiguration];
}
}
@end