Replicate photo booth screen flash in cocoa
Asked Answered
P

2

5

I am trying to replicate a screen flash effect in a mac cocoa application similar to that of the Photo Booth.

A white layer is overlayed on the screen and the brightness of the screen fades really bright and then down again.

Can anyone give me some advice on how this can be replicated in Cocoa?

Thanks

Prohibitionist answered 5/1, 2011 at 9:46 Comment(0)
S
6

I suggest using the CGDisplayFade API of Quartz Display Services. It's very easy to use and no "hacking" with fake fullscreen windows or views is required.

See here: Quartz Display Services Reference

A simple implementation would look like this:

-(void)flashScreenUsingFlashColor:(NSColor *)flashColor
                   inDuration:(NSTimeInterval)inDuration
                  outDuration:(NSTimeInterval)outDuration{

CGDisplayFadeReservationToken fadeToken;
NSColor *colorToUse = [flashColor colorUsingColorSpaceName: NSCalibratedRGBColorSpace];

CGError error = CGAcquireDisplayFadeReservation (inDuration + outDuration, &fadeToken);
if (error != kCGErrorSuccess){
    NSLog(@"Error aquiring fade reservation. Will do nothing.");
    return;
}

CGDisplayFade (fadeToken, inDuration, kCGDisplayBlendNormal, kCGDisplayBlendSolidColor, colorToUse.redComponent, colorToUse.greenComponent, colorToUse.blueComponent, true);
CGDisplayFade (fadeToken, outDuration, kCGDisplayBlendSolidColor, kCGDisplayBlendNormal,colorToUse.redComponent, colorToUse.greenComponent, colorToUse.blueComponent, false);

}
Strontian answered 8/7, 2013 at 16:7 Comment(2)
here is the Swift version: gist.github.com/128keaton/81da4399f9d005b9b4ae96924f246663. Seems like doesn't work on macOS 12.5.1 anymore. The CGError says .notImplementedPazice
The linked Swift implementation works for me on macOS 12.6.Glandular
S
1

You could take a look at this tutorial for creating a full screen window. Just make it white and the use Core Animation to fade it in and out. For example: [[MyFullScreenWindow animator] setAlphaValue:0.0]; will fade it out.

Softcover answered 19/1, 2011 at 1:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.