iOS中屏幕亮度与闪光灯控制详解
程序员文章站
2023-12-19 19:07:52
本文主要介绍的是关于ios屏幕亮度与闪光灯控制的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:
所用涉及框架:avfoundation框架和imageio
读...
本文主要介绍的是关于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]; } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。