欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

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

时间:2021-01-06 11:22:45|栏目: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]; 
 }
}

总结

上一篇:iOS键盘如何添加隐藏键盘功能

栏    目:iOS代码

下一篇:探究iOS多线程究竟不安全在哪里?

本文标题:iOS中屏幕亮度与闪光灯控制详解

本文地址:http://www.codeinn.net/misctech/41329.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有