iOS中屏幕亮度与闪光灯控制详解

本文主要介绍的是关于iOS屏幕亮度与闪光灯控制的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:

所用涉及框架:AVFoundation框架和ImageIO

读取屏幕亮度: [UIScreen mainScreen].brightness;

设置屏幕亮度: [[UIScreen mainScreen] setBrightness:0.5];

获取环境亮度主要代码:

- (void)getTorch {
 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
 AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
 AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
 [output setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
 self.session = [[AVCaptureSession alloc]init];
 [self.session setSessionPreset:AVCaptureSessionPresetHigh];
 if ([self.session canAddInput:input]) {
  [self.session addInput:input];
 }
 if ([self.session canAddOutput:output]) {
  [self.session addOutput:output];
 }
 [self.session startRunning];

}

- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection {
 CFDictionaryRef metadataDict =CMCopyDictionaryOfAttachments(NULL,sampleBuffer, 
 kCMAttachmentMode_ShouldPropagate);
 NSDictionary *metadata = [[NSMutableDictionary alloc] initWithDictionary:
 (__bridgeNSDictionary*)metadataDict];

 CFRelease(metadataDict);
 NSDictionary *exifMetadata = [[metadata objectForKey:(NSString*)kCGImagePropertyExifDictionary] mutableCopy];
 float brightnessValue = [[exifMetadata objectForKey:(NSString*)kCGImagePropertyExifBrightnessValue] floatValue];
 NSLog(@"%f",brightnessValue);
 // 根据brightnessValue的值来打开和关闭闪光灯
 AVCaptureDevice*device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
 BOOL result = [device hasTorch];// 判断设备是否有闪光灯
 if((brightnessValue <0) && result) {
 // 打开闪光灯
  [device lockForConfiguration:nil];
  [device setTorchMode:AVCaptureTorchModeOn];//开
  [device unlockForConfiguration];
 }else if((brightnessValue >0) && result) {
 // 关闭闪光灯
  [device lockForConfiguration:nil];  
  [device setTorchMode:AVCaptureTorchModeOff];//关
  [device unlockForConfiguration]; 
 }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对呐喊教程的支持。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。