简单介绍IOS开发中屏幕亮度和闪光控制
来源:爱站网时间:2021-03-23编辑:网友分享
项目开发中我们发现需要控制屏幕的亮度,而拥有控制亮度的空间要怎么进行区分?文中是爱站技术频道小编带给大家的简单介绍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:AVCaptureTorchModeOff];//关 [device unlockForConfiguration]; } }
简单介绍IOS开发中屏幕亮度和闪光控制,大家了解的怎样了呢?其实很多的编程操作大家也都会操作,希望大家能够选择适合自己的操作。