iOS中屏幕亮度与闪光灯控制详解
本文主要介绍的是关于iOS屏幕亮度与闪光灯控制的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:
所用涉及框架:AVFoundation框架和ImageIO
读取屏幕亮度:[UIScreenmainScreen].brightness;
设置屏幕亮度:[[UIScreenmainScreen]setBrightness:0.5];
获取环境亮度主要代码:
-(void)getTorch{
AVCaptureDevice*device=[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput*input=[[AVCaptureDeviceInputalloc]initWithDevice:deviceerror:nil];
AVCaptureVideoDataOutput*output=[[AVCaptureVideoDataOutputalloc]init];
[outputsetSampleBufferDelegate:selfqueue:dispatch_get_main_queue()];
self.session=[[AVCaptureSessionalloc]init];
[self.sessionsetSessionPreset:AVCaptureSessionPresetHigh];
if([self.sessioncanAddInput:input]){
[self.sessionaddInput:input];
}
if([self.sessioncanAddOutput:output]){
[self.sessionaddOutput:output];
}
[self.sessionstartRunning];
}
-(void)captureOutput:(AVCaptureOutput*)captureOutputdidOutputSampleBuffer:(CMSampleBufferRef)sampleBufferfromConnection:(AVCaptureConnection*)connection{
CFDictionaryRefmetadataDict=CMCopyDictionaryOfAttachments(NULL,sampleBuffer,
kCMAttachmentMode_ShouldPropagate);
NSDictionary*metadata=[[NSMutableDictionaryalloc]initWithDictionary:
(__bridgeNSDictionary*)metadataDict];
CFRelease(metadataDict);
NSDictionary*exifMetadata=[[metadataobjectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
floatbrightnessValue=[[exifMetadataobjectForKey:(NSString*)kCGImagePropertyExifBrightnessValue]floatValue];
NSLog(@"%f",brightnessValue);
//根据brightnessValue的值来打开和关闭闪光灯
AVCaptureDevice*device=[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
BOOLresult=[devicehasTorch];//判断设备是否有闪光灯
if((brightnessValue<0)&&result){
//打开闪光灯
[devicelockForConfiguration:nil];
[devicesetTorchMode:AVCaptureTorchModeOn];//开
[deviceunlockForConfiguration];
}elseif((brightnessValue>0)&&result){
//关闭闪光灯
[devicelockForConfiguration:nil];
[devicesetTorchMode:AVCaptureTorchModeOff];//关
[deviceunlockForConfiguration];
}
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。