klcjr89
klcjr89

Reputation: 5902

iOS UIImagePickerController front facing flash

I've been trying to implement front facing flash (Yes I know it will be crappy quality pretty much). I'm having a problem with trying to get my screen's brightness and added white subview to time just right, because the returned image never has the flash applied in the image.

I appreciate any help offered.

Code:

if (self.imagePicker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
{
    if ([self.flashButton.titleLabel.text isEqualToString:@"On"]
    {
        CGFloat oldBrightness = [UIScreen mainScreen].brightness;

        UIWindow *window = [UIApplication sharedApplication].keyWindow;

        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width, window.frame.size.height)];

        [UIView animateWithDuration:1.0f
                                  delay:0.0
                                options:UIViewAnimationOptionCurveLinear
                             animations:^
         {
             [self.imagePicker takePicture];
             [window addSubview:view];
             [[UIScreen mainScreen]setBrightness:1.0]
             view.backgroundColor = [UIColor whiteColor];
             view.alpha = 1.0f;
         }
         completion:^(BOOL finished)
         {
            if (finished) 
            {
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationBeginsFromCurrentState:YES];
                [UIView setAnimationCurve:UIViewAnimationCurveLinear];
                [UIView setAnimationDuration:1.0f];
                view.alpha = 0.0;
                [view removeFromSuperview];
                [[UIScreen mainScreen]setBrightness:oldBrightness];
                [UIView commitAnimations];
            }
         }];
    }
    else
    {
        [self.imagePicker takePicture];
    }
}

Upvotes: 0

Views: 380

Answers (1)

Bamsworld
Bamsworld

Reputation: 5680

I suggest moving [self.imagePicker takePicture]; to the start of your completion handler.

The animation block will take 1 sec to reach full brightness and it is immediately after this that you will want the picture taken.

Upvotes: 1

Related Questions