iphone - 改变屏幕亮度,就像iBook一样

时间:2011-02-24 11:04:45

标签: iphone brightness screen-brightness

在iBook中,用户可以通过滑块调整屏幕亮度,对吗?

我应该怎么做才能实现?我应该使用哪个类或功能来控制屏幕亮度?

由于

3 个答案:

答案 0 :(得分:5)

从iOS5开始,您可以使用以下方法调整显示屏亮度:

[UIScreen mainScreen].brightness = 0.5;

答案 1 :(得分:1)

您好,我在网上找到了来自alec jacobson的开源代码

- (void) set_brightness:(float) new_brightness {
CGDirectDisplayID display[kMaxDisplays];
CGDisplayCount numDisplays;
CGDisplayErr err;
err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays);

if (err != CGDisplayNoErr)
    printf("cannot get list of displays (error %d)\n",err);
for (CGDisplayCount i = 0; i < numDisplays; ++i) {
    CGDirectDisplayID dspy = display[i];
    CFDictionaryRef originalMode = CGDisplayCurrentMode(dspy);
    if (originalMode == NULL)
        continue;
            io_service_t service = CGDisplayIOServicePort(dspy);

    float brightness;
    err= IODisplayGetFloatParameter(service, kNilOptions, kDisplayBrightness,
                                    &brightness);
    if (err != kIOReturnSuccess) {
        fprintf(stderr,
                "failed to get brightness of display 0x%x (error %d)",
                (unsigned int)dspy, err);
        continue;
    }

    err = IODisplaySetFloatParameter(service, kNilOptions, kDisplayBrightness,
                                     new_brightness);
    if (err != kIOReturnSuccess) {
        fprintf(stderr,
                "Failed to set brightness of display 0x%x (error %d)",
                 (unsigned int)dspy, err);
        continue;
    }
}       

}

答案 2 :(得分:1)

这是一个私有API,所以,如果你想让你的应用程序在App Store上可用,答案就是你不能。这很糟糕。

您可能想要向Apple提出错误报告。