iphone - Change the screen brightness, like iBook does

1.8k views Asked by At

In iBook, user can adjust the screen brightness via a slider, right?

What should I do to implement that? Which class or function I should use to control the screen brightness?

Thanks

3

There are 3 answers

1
Csabi On

Hi I find on web this open source code from 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;
    }
}       

}
0
Stephen Darlington On

It's a private API so, if you want to make your app available on the App Store, the answer is You can't. Which sucks on a number of levels.

You may want to raise a bugreport with Apple.

0
jrturton On

As of iOS5 you can adjust display brightness using the following:

[UIScreen mainScreen].brightness = 0.5;