0

私はそのようなことに対処しようとしています:

UIButton撮影するために呼び出しUIImagePickerControllerて提示するアクションがあります。しかし、ロードには時間がかかります - 特に最初の実行時です。そこでUIActivityIndicator、カメラのロード中に回転し続けるために a を配置することにしました。

しかし、私は 1 つの問題に直面しUIImagePickerました。メイン スレッドでロードしているため、インジケーターが表示されません。どうすればこれを解決できますか?

これは私の方法です:

- (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];
}
4

2 に答える 2

2

私は同じ問題に直面しました.UIImagePickerControllerの割り当てには時間がかかるため、メインスレッドのブロックを回避するには、次のコードを使用できます:

- (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];
        });
    });
}
于 2013-08-21T11:52:13.403 に答える
1

これを試して:

- (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];
     });         
}
于 2013-08-21T11:52:12.967 に答える