Accessing the ambient light sensor in iOS

前端 未结 2 520
情书的邮戳
情书的邮戳 2021-02-07 13:32

I\'m working on a project in which it is really necessary to access the ambient light sensor.

I searched a lot in Google and Stackoverflow, but couldn\'t find any usefu

2条回答
  •  独厮守ぢ
    2021-02-07 14:08

    I solve this problem With access to the camera

    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection: (AVCaptureConnection *)connection
    {
     CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL,
    sampleBuffer, kCMAttachmentMode_ShouldPropagate);
    NSDictionary *metadata = [[NSMutableDictionary alloc]
                              initWithDictionary:(__bridge NSDictionary*)metadataDict];
    CFRelease(metadataDict);
    NSDictionary *exifMetadata = [[metadata objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy];
    float brightnessValue = [[exifMetadata objectForKey:(NSString *)kCGImagePropertyExifBrightnessValue] floatValue];
    
    //THIS IS INFORMATION THAT COMES FROM THE SENSOR
    _Sensor = [[NSNumber numberWithFloat:brightnessValue] stringValue];
     NSLog(@" %@",_Sensor);
    
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    
    
            if ([_Sensor isEqualToString:@"-5.575654"]) {
    
           // YOU CODE HER
    
            }
            else {
    
           // YOU CODE HER
        }
    
    });
    
    }
    

提交回复
热议问题