这篇文章主要介绍iOS中屏幕亮度与闪光灯控制的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
专注于为中小企业提供成都网站建设、成都网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业进贤免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
所用涉及框架: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中屏幕亮度与闪光灯控制的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!