IOS打开设备手电筒

    导入#import<AVFoundation/AVFoundation.h>

   @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];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容