Pregunta

I'm trying to deal with such a thing:

I have a UIButton action which is calling UIImagePickerController and presenting it to tak a shoot. But the loading is taking a while - especially at the first run. So I've decided to put a UIActivityIndicatorto keep spinning while loading the Camera.

But I faced one problem - UIImagePicker is loading in main thread so the indicator won't show. How can I solve this?

This is my method:

- (IBAction)takePhoto:(UIButton *)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityView.center=self.view.center;
    [self.view addSubview:activityView];
    [activityView startAnimating];

    [self presentViewController:imagePicker animated:NO completion:nil];
}
¿Fue útil?

Solución

I've faced same problem, UIImagePickerController allocation takes much time, so to avoid main thread blocking you can use next code:

- (IBAction)takePhoto:(UIButton *)sender
{
    UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityView.center=self.view.center;
    [self.view addSubview:activityView];
    [activityView startAnimating];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        dispatch_async(dispatch_get_main_queue(), ^{
            [self presentViewController:imagePicker animated:NO completion:nil];
        });
    });
}

Otros consejos

Try this:

- (IBAction)takePhoto:(UIButton *)sender
{
     UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
     activityView.center=self.view.center;
     [self.view addSubview:activityView];
     [activityView startAnimating];

     int64_t delayInSeconds = 2.0;//How long do you want to delay?
     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
     dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

         UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
         imagePicker.delegate = self;
         imagePicker.allowsEditing = YES;
         imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

         [self presentViewController:imagePicker animated:NO completion:nil];
     });         
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top