Currently, I am using this code to open camera & video view for capturing image & video:
- (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;
}
It’s working fine. But I want to open camera view repeatedly and when the Use button is clicked I want to save the image to directory. This is what I am trying:
- (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];
}
}
But can’t succeed in opening the camera repeatedly. Here is an error I get:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <ViewController: 0x49bab0>.'
and when I am not using a timer then this is what I am doing:
- (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];
}
}
I am stuck at this point. How can I solve this? Thanx in advance.
I think the error says that, you are trying to present a view controller modally, which is already displayed…
The issue may be, you are trying to open the camera view EVERY 10 seconds, however it may create problem if the existing displayed view is not dismissed at that time.
Instead of using timer, show the picker again manually only after you dismiss it.
You can have an instance variable to keep track of how many times it has been already shown.