Niharika
Niharika

Reputation: 1198

Open Camera Instantly when view appears

I have a requirement in my app in which I need to open a camera instantly. But when I come to that ViewController opening camera is taking time for few seconds. Can any one help me. I have tried with queues blocks but this doesn't work. This is the code i have tried so far :==

    - (void)viewDidLoad {

            [super viewDidLoad];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
                [self onClickCamera:nil];

                //Your code goes in here
                NSLog(@"Main Thread Code");

            }];
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [self onClickCamera:nil];
//    });

//    dispatch_queue_t queue = dispatch_queue_create("com.example.MyQueue", NULL);
//    dispatch_async(queue, ^{
//        [self onClickCamera:nil];
//
//        // Do some computation here.
//        
//        // Update UI after computation.
//        dispatch_async(dispatch_get_main_queue(), ^{
//            // Update the UI on the main thread.
//        });
//    });


    }

        - (IBAction)onClickCamera:(id)sender
        {
            NSLog(@"CALeed");
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = NO;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:NULL];
        }

Upvotes: 0

Views: 249

Answers (2)

iOS_MIB
iOS_MIB

Reputation: 1895

I agree with @SandeepBhandari's answer. You can make change to the following line apart from calling the method to present camera in viewDidAppear() to reduce the delay:

Change

[self presentViewController:picker animated:YES completion:NULL];

to

[self presentViewController:picker animated:NO completion:NULL];

Adding more to this you can predefine your pickerController globally and set all it's properties and parameters in viewDidLoad()

- (void)viewDidLoad{

    picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = NO;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

}

Also initially you can set the backgroundColour of your viewController's main view to black or similar to the colour of your launchScreen to avoid seeing that glitches when camera is presented.

You can also use CATransition to reduce time like follows

CATransition *transition = [CATransition animation];
    [transition setDuration:0];
    [transition setType:kCATransitionFromBottom];
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [picker.view.layer addAnimation:transition forKey:kCATransitionFromBottom];

and then present the picker controller.

Well this is yet not guaranteed to give you desire results as presenting something will obviously take some fraction of time. If you have a good usage of camera in your app then i would suggest to use custom class for it and use AVCaptureVideoPreviewLayer to show the camera.

Upvotes: 0

Sandeep Bhandari
Sandeep Bhandari

Reputation: 20379

Two issues :

Issue 1:

In viewDidLoad view has not yet finished rendering its layout yet. So trying to present something over a view which is still rendering, will obviously add the delay. Presenting camera will be triggered only after view finishes laying out its layout.

So move the code from viewDidLoad to viewDidAppear

 - (void) viewDidAppear {
     [super viewDidAppear];
     [self onClickCamera:nil];
 }

Issue 2:

More of a warning then error.

why even [NSOperationQueue mainQueue] addOperationWithBlock: ?? ViewDidLoad always gets called on main thread only which is associated with serialized main queue itself. The un-necessary context switching makes no much sense

Upvotes: 1

Related Questions