获得显示亮度

时间:2015-03-02 20:35:30

标签: macos cocoa osx-mavericks

有没有办法在OS X 10.9+中获得显示亮度,因为CGDisplayIOServicePort已被弃用?

2 个答案:

答案 0 :(得分:1)

经过一番搜索和摆弄,这是一个未来的证据"获取OS X 10.9中不推荐使用的CGDisplayIOServicePort的显示亮度的方法。

- (float)getDisplayBrightness
{
    float brightness = 1.0f;
    io_iterator_t iterator;
    kern_return_t result = 
        IOServiceGetMatchingServices(kIOMasterPortDefault,
            IOServiceMatching("IODisplayConnect"),
            &iterator);

    // If we were successful
    if (result == kIOReturnSuccess)
    {
        io_object_t service;

        while ((service = IOIteratorNext(iterator)))
        {
            IODisplayGetFloatParameter(service, 
                kNilOptions, 
                CFSTR(kIODisplayBrightnessKey), 
                &brightness);

            // Let the object go
            IOObjectRelease(service);
        }
    }

    return brightness;
}

答案 1 :(得分:0)

如果您使用的是swift,则应该可以进行以下操作:

func getDisplayBrightness() -> Float {

    var brightness: Float = 1.0
    var service: io_object_t = 1
    var iterator: io_iterator_t = 0
    let result: kern_return_t = IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IODisplayConnect"), &iterator)

    if result == kIOReturnSuccess {

        while service != 0 {
            service = IOIteratorNext(iterator)
            IODisplayGetFloatParameter(service, 0, kIODisplayBrightnessKey as CFString, &brightness)
            IOObjectRelease(service)
        }
    }
    return brightness
}