IOS打开设备手电筒

    导入#import

   @interface MobileViewControllers (){

    BOOL LightOn;

    AVCaptureDevice *device;

 }

@implementation MobileViewControllers

- (void)viewDidLoad {

[super viewDidLoad];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btn setFrame:CGRectMake(100, 150, 100, 80)];

btn.backgroundColor = [UIColor redColor];

[btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

[self initUIIIUI];

}

-(void)initUIIIUI{

device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

if (![device hasTorch]) {

//无手电筒

}

LightOn = NO;

}

-(void)btnClicked

{

LightOn = !LightOn;

if (LightOn) {

[self turnOn];

}else{

[self turnOff];

}

}


-(void) turnOn

{

[device lockForConfiguration:nil];

[device setTorchMode:AVCaptureTorchModeOn];

[device unlockForConfiguration];

}

-(void) turnOff

{

[device lockForConfiguration:nil];

[device setTorchMode: AVCaptureTorchModeOff];

[device unlockForConfiguration];

}

你可能感兴趣的:(IOS打开设备手电筒)