您好,登錄后才能下訂單哦!
這篇文章主要介紹iOS如何實現(xiàn)屏幕亮度和閃光燈控制功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
所用涉及框架:AVFoundation框架和ImageIO
讀取屏幕亮度:[UIScreen mainScreen].brightness;
設置屏幕亮度:[[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的值來打開和關閉閃光燈 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如何實現(xiàn)屏幕亮度和閃光燈控制功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。