1

目前,我正在使用此代码打开相机和视频视图以捕获图像和视频:

- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
                                   usingDelegate: (id <UIImagePickerControllerDelegate,
                                                   UINavigationControllerDelegate>) delegate
{
    [optionView removeFromSuperview];

    if (([UIImagePickerController isSourceTypeAvailable:
          UIImagePickerControllerSourceTypeCamera] == NO)
        || (delegate == nil)
        || (controller == nil))
        return NO;

    if (videoModeFlag==TRUE)
    {
        cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
        cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
        cameraUI.allowsEditing = NO;
        cameraUI.delegate = delegate;
        [self presentViewController:cameraUI animated:YES completion:nil];
        videoModeFlag=FALSE;
    }
    else
    {
        photoclick.sourceType = UIImagePickerControllerSourceTypeCamera;
        photoclick.delegate = self;
        [self presentViewController:photoclick animated:YES completion:nil];
    }

    return YES;
}

它工作正常。但是我想反复打开相机视图,当单击使用按钮时,我想将图像保存到目录中。这就是我正在尝试的:

- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
{
    saveFlag =TRUE;
    [self.view addSubview:typename];
    image = [[info objectForKey:UIImagePickerControllerOriginalImage] copy];
    mediaType = [[info objectForKey: UIImagePickerControllerMediaType] copy];
    movieURL = [[info valueForKey:UIImagePickerControllerMediaURL] copy];
    [self dismissViewControllerAnimated:YES completion:nil];

        timer = [NSTimer scheduledTimerWithTimeInterval:10.0
                                                 target:self
                                               selector: @selector(targetMethod)
                                               userInfo:nil
                                                repeats:YES];
- (void)targetMethod
{

    a++;                                                                             

        [self startCameraControllerFromViewController: self
                                        usingDelegate: self];


    if (a==5)
    {
        [timer invalidate];
    }
}

但无法成功多次打开相机。这是我得到的一个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <ViewController: 0x49bab0>.'

当我不使用计时器时,这就是我正在做的事情:

- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
{
    saveFlag =TRUE;
    //[self.view addSubview:typename];
    image = [[info objectForKey:UIImagePickerControllerOriginalImage] copy];
    mediaType = [[info objectForKey: UIImagePickerControllerMediaType] copy];
    movieURL = [[info valueForKey:UIImagePickerControllerMediaURL] copy];

    [self dismissViewControllerAnimated:YES completion:nil];

    NSMutableArray *sd=[[NSMutableArray alloc]init];
    sd = [[getPickerData componentsSeparatedByString: @"\n--- end of page ---\n"] mutableCopy];
    NSLog(@"sd:%@",sd);
    for (int i=0; i<=[sd count]; i++)
    {
        [self startCameraControllerFromViewController: self
                                        usingDelegate: self];
    }
}

我被困在这一点上。我该如何解决这个问题?提前谢谢。

4

1 回答 1

0

我认为错误表明,您正在尝试以模态方式呈现视图控制器,该视图控制器已经显示...

问题可能是,您尝试每 10 秒打开一次相机视图,但是如果当时未关闭现有显示的视图,则可能会产生问题。

与其使用计时器,不如仅在您将其关闭后再次手动显示选择器。您可以有一个实例变量来跟踪它已经显示了多少次。

于 2012-12-26T12:11:59.893 回答