2012-12-26 36 views
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]; 
    } 
} 

我停留在這一點上。我該如何解決這個問題?提前Thanx。

回答

0

我認爲錯誤說,你正在試圖提出一個視圖控制器模態,這已經顯示...

的問題可能是,你試圖打開攝像機視圖每10秒,但是如果現有的顯示視圖當時沒有被解僱,它可能會產生問題。

除了使用計時器之外,只有在您解僱後才能手動再次顯示選取器。 您可以有一個實例變量來跟蹤它已經顯示的次數。

+0

我正在嘗試使用Self方法來調用捕獲方法,但問題是相同的...... – Vishal

+0

您能否更新修改後的代碼? –

+0

我更新我的問題... – Vishal