目前,我正在使用此代码打开相机和视频视图以捕获图像和视频:
- (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];
}
}
我被困在这一点上。我该如何解决这个问题?提前谢谢。