iOS中屏幕亮度與閃光燈控制詳解
本文主要介紹的是關(guān)于iOS屏幕亮度與閃光燈控制的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來看看詳細的介紹:
所用涉及框架: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];
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
IOS 播放系統(tǒng)提示音使用總結(jié)(AudioToolbox)
這篇文章主要介紹了IOS 播放系統(tǒng)提示音使用總結(jié)(AudioToolbox)的相關(guān)資料,需要的朋友可以參考下2017-05-05
iOS中setValue和setObject的區(qū)別詳解
setObject:ForKey: 是NSMutableDictionary特有的;setValue:ForKey:是KVC的主要方法。接下來通過本文給大家分享iOS中setValue和setObject的區(qū)別,需要的朋友參考下2017-02-02
CocoaPods 出現(xiàn)LoadError - cannot load such file -- nanaimo錯誤解決
這篇文章主要介紹了CocoaPods 出現(xiàn)LoadError - cannot load such file -- nanaimo錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04

