From Apple Documentation.
"This supports taking more than one picture without leaving the interface, but requires that you hide the default image picker controls".
On [self.imagePicker takePicture] the delegate didFinishPickingMediaWithInfo is being called but is still leaving the interface.
Any idea how can i prevent the controller from being dismissed?Here is my code of UIImagePickerController with custom overlay.
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = NO;
CameraOverlayView *overlay = (CameraOverlayView *)[self viewWithNibName:#"CameraOverlayView" owner:self];
overlay.frame = imagePicker.cameraOverlayView.frame;
imagePicker.cameraOverlayView = overlay;
overlay.imagePicker = imagePicker;
[self presentViewController:imagePicker animated:YES completion:nil];
overlay = nil;
I have same problem but i resolved this by using these classes in my code UzysAssetsPickerController for iOS
By using this i can select multiple images without dismissing the image picker. Hope this will help.
This solved my problem.Working perfectly now.Thank You everyone.
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
#property(nonatomic,strong) UIImagePickerController *imagePicker;
overlay.imagePicker = _imagePicker;
overlay.imagePicker.delegate = overlay;
// may be it will help for you.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// if you want to dismiss controll then use the following code
[picker dismissViewControllerAnimated:YES completion:^{
}];
// if you wnat to being present it comment the above dismis code
}
Related
This seems like a simple question and I've seen similar ones answered but the answers just don't work (at least anymore). I have the following code in viewDidAppear:
UIImagePickerController *imagePicker;
imagePicker =[[UIImagePickerController alloc] init];
imagePicker.allowsEditing = YES;
{imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;}
imagePicker.delegate=self;
NSLog(#"ViewDidAppear");
[self presentModalViewController:imagePicker animated:YES];
NSLog (#"Modal Window Displayed.");}
This allows the user to select an image from the image library, which works. Once that image is picked, didFinishPickingMediaWithInfo gets called which has this code at the top of it:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(#"Image Is Picked");
[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
[self dismissModalViewControllerAnimated:YES];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popViewControllerAnimated:YES];
As you can see, I've tried several different ways found here on StackOverflow to get this to work. They all get ignored. No errors, just nothing happens; the picker view never goes away. What gives? I'm using xCode 7.2.1.
I have a similar place in my code where I present another modal view and also can't get rid of it.
The problem is that the didFinishPickingMediaWithInfo method doesn't actually dismiss the other view until it is DONE with it's execution (even if what's running in that method takes almost a minute!). By the time that method was done, I had already presented another viewcontroller modally. Almost all of the techniques I'd been trying all along actually still work.
If you present with presentViewController instead of presentModalViewController:
[self presentViewController:imagePicker animated: YES];
then
[self dismissViewControllerAnimated:YES completion:nil];
should work - perhaps the combination of different functions you are calling all at once is confusing things.
If this doesn't work, perhaps try presenting it with a slight delay with dispatch_after - it could be that presenting it within viewDidAppear is causing the issue:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.001 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
[self presentViewController:imagePicker animated: YES];
}
Below code works for me perfectly. Hope it helps.
in class.h
#interface class : UIViewController<UIImagePickerControllerDelegate>
{
UIImagePickerController *picker;
}
in class.m when getPhoto called, PickerViewController presented.
- (IBAction)getPhoto:(UIButton *)sender {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker2 didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker2 dismissViewControllerAnimated:YES completion:nil];
}
Globally declare your imagePicker like below
#implementation ViewController{
UIImagePickerController *imagePicker;
}
Display your image picker declared globally
- (IBAction)chooosePic:(id)sender {
imagePicker =[[UIImagePickerController alloc] init];
imagePicker.allowsEditing = YES; {imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;}
imagePicker.delegate=self;
NSLog(#"ViewDidAppear");
[self presentModalViewController:imagePicker animated:YES];
NSLog (#"Modal Window Displayed.");
}
didFinishPickingMediaWithInfo method update as below:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[imagePicker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Will Work
So what am I trying to do here is just to open the camera roll from within a custom camera overlayView. I implemented the custom camera overlayView, created a UIImagePickerController, and replaced the default overlay with my custom one. Then I created another instance of UIImagePickerController, set the sourceType to PhotoLibrary and tries to present it when a button on the overlayView is pressed.
The below error message shows when i press the button:
Warning: Attempt to present <UIImagePickerController: 0x14da5440> on <UINavigationController: 0x14e63760> whose view is not in the window hierarchy!
Here is my code:
- (IBAction)select:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker
animated:YES completion:nil];
}
}
Does anyone know what's the problem here?
Thanks!
you need to use completion block for present one view controller when another view controller dismiss like following:
[self dismissViewControllerAnimated:YES completion:^{
[self presentViewController:imagePicker
animated:YES completion:nil];
}];
How can I open camera inside my app controller.
I don't want to present built in camera controller.
I want to make my own camera sort of app.
Any suggestion or guide will be highly appreciated.
You can easily use UIImagePickerController for this
Declare UIImagePickerController *imagePickerController; globally for the class
Declare protocols
#interface YourClassName : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
imagePickerController = [[UIImagePickerController alloc] init];
[imagePickerController setDelegate:self];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePickerController setMediaTypes:#"public.image"]]; //specify image (not video)
[imagePickerController setShowsCameraControls:NO]; //hide default camera controls
[imagePickerController setNavigationBarHidden:YES];
[imagePickerController setToolbarHidden:YES];
[imagePickerController setAllowsEditing:NO];
[self.view addSubview:imagePickerController.view];
Use the takePicture method for the UIImagePickerController object like and assign it on a button action, like:
-(IBAction)btnTakePictureAct:(UIButton *)sender
{
[imagePickerController takePicture];
}
Use the delegate -imagePickerController:didFinishPickingMediaWithInfo: to get the image.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = info[UIImagePickerControllerOriginalImage];
}
See this, get an idea, make your custom camera controls.
Refer: UIImagePickerController Apple Doc
I'm creating an app that is landscape only, it uses an image picker control.
Looking through the site I've discovered that apple only allow portrait for this for some reason. I'm okay with it flipping to portrait for this one section, if it means the user can select a photo from the library.
Below is my code that gives an error about it being in landscape mode. How do I fix this to say it's okay to flip it to portrait. thanks
-(IBAction)takePhoto{
takePicker = [[UIImagePickerController alloc]init];
takePicker.delegate = self;
[takePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:takePicker animated:YES completion:NULL];
}
-(IBAction)chooseExisiting{
choosePicker = [[UIImagePickerController alloc]init];
choosePicker.delegate = self;
[choosePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:choosePicker animated:YES completion:NULL];
}
You don't have to use ImagePickerController in portrait mode.
Just subclass it to open in landscape mode :
#interface NonRotatingUIImagePickerController : UIImagePickerController
#end
#implementation NonRotatingUIImagePickerController
- (BOOL)shouldAutorotate
{
return NO;
}
#end
After that, you can use your code with that class.
-(IBAction)takePhoto {
takePicker = [[NonRotatingUIImagePickerController alloc]init];
takePicker.delegate = self;
[takePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:takePicker animated:YES completion:NULL];
}
-(IBAction)chooseExisiting{
choosePicker = [[NonRotatingUIImagePickerController alloc]init];
choosePicker.delegate = self;
[choosePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:choosePicker animated:YES completion:NULL];
}
Why present an error at all? Why not just present the image picker in portrait? People are smart enough to rotate their device. Or you could write your own landscape image picker, or use one of the several open source ones available.
Here is my code:
-(void) takePhoto
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//imagePickerController.editing = YES;
imagePickerController.allowsEditing=YES;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:NULL];
}
#pragma mark - Image picker delegate methods
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:NO completion:nil];
[self.Picture setImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
}
and I have implemented the delegates
UINavigationControllerDelegate,UIImagePickerControllerDelegate
The image is taken, I can see the move and scale box, but when I move it the box returns to the initial position - likes it bounces back.
Why is that?
Looks like this is caused because you are requesting the UIImagePickerControllerOriginalImage, according to the documentation this returns "the original, uncropped image selected by the user."
Try changing this to UIImagePickerControllerEditedImage.
https://developer.apple.com/library/ios/documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html#//apple_ref/doc/constant_group/Editing_Information_Keys
Edit: Actually after re-reading the question a couple of times if the problem is occurring in the picker itself this probably won't help.