目前,我正在使用此代码打开相机和相机用于捕获图像和视频的视频视图视频:
- (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];
}
}
我在这一点上陷入困境。我怎么解决这个问题?提前完成。
答案 0 :(得分:0)
我认为错误说明了,你试图以模态方式呈现视图控制器,已经显示...
问题可能是,您尝试每隔10秒打开一次摄像机视图,但是如果当时没有关闭现有的显示视图,则可能会产生问题。
取代使用计时器,只有在解雇后再次手动显示选择器。 您可以拥有一个实例变量来跟踪它已经显示的次数。