iOS實(shí)現(xiàn)屏幕亮度和閃光燈控制的實(shí)例代碼
這兩天學(xué)習(xí)了iOS屏幕亮度和閃光燈控制,所以,今天添加一點(diǎn)小筆記。
所用涉及框架:AVFoundation框架和ImageIO
讀取屏幕亮度:[UIScreen mainScreen].brightness;
設(shè)置屏幕亮度:[[UIScreen mainScreen] setBrightness:0.5];
獲取環(huán)境亮度主要代碼:
- (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); // 根據(jù)brightnessValue的值來打開和關(guān)閉閃光燈 AVCaptureDevice*device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; BOOL result = [device hasTorch];// 判斷設(shè)備是否有閃光燈 if((brightnessValue <0) && result) { // 打開閃光燈 [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOn];//開 [device unlockForConfiguration]; }else if((brightnessValue >0) && result) { // 關(guān)閉閃光燈 [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOff];//關(guān) [device unlockForConfiguration]; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS11實(shí)現(xiàn)App內(nèi)自動(dòng)連接Wi-Fi的方法
這篇文章主要給大家介紹了關(guān)于iOS11實(shí)現(xiàn)App內(nèi)自動(dòng)連接Wi-Fi的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10iOS開發(fā)之widget實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)之widget實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09IOS動(dòng)畫效果源代碼整理(粒子、雪花、火焰、河流、蒸汽)
本篇文章給大家整理的IOS的關(guān)于動(dòng)畫的效果代碼整理,很多效果非常的好看,有興趣的學(xué)下。2018-01-01iOS實(shí)現(xiàn)MJRefresh下拉刷新(上拉加載)使用詳解
本篇文章主要介紹了iOS實(shí)現(xiàn)MJRefresh下拉刷新(上拉加載)使用詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01