I created a simple button within my Main-View Controller. After calling imagePickerController and taking a picture, I want to redirect the user to the next view controller to continue processing the image. Somehow it always returns to my main window.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;
if (_newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
// Code here to support video if enabled
}
}
for that you have to push your view Like
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
...
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
...
YourImgViewController *imgProcessing = [[YourImgViewController alloc] initWithNibName:#"YourImgViewController" bundle:nil];
imgProcessing.selectedImageView.image = image;
[self.navigationController pushViewController:imgProcessing animated:YES];
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
// Code here to support video if enabled
}
}
very simple!
you can find more hint on this link http://www.edumobile.org/iphone/how-to-make-an-app-2/tutorial-on-image-picker-controller-in-iphone/
Related
Is the following creating a strong reference cycle? I have a feeling it is because I'm referencing self within callback.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *image = [self cropImageWithInfo:info];
if (currentScreen == CurrentScreenApartment) {
[self.profileViewModel.apartmentPhotos addObject:[RuntimePhoto runtimePhotoWithImage:image]];
}
else {
[self.profileViewModel.userPhotos addObject:[RuntimePhoto runtimePhotoWithImage:image]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self reloadPhotosCell];
});
}];
}
You can use a weak variable of self:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
typeof(self) __weak weakSelf = self;
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *image = [weakSelf cropImageWithInfo:info];
if (currentScreen == CurrentScreenApartment) {
[weakSelf.profileViewModel.apartmentPhotos addObject:[RuntimePhoto runtimePhotoWithImage:image]];
}
else {
[weakSelf.profileViewModel.userPhotos addObject:[RuntimePhoto runtimePhotoWithImage:image]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf reloadPhotosCell];
});
}];
}
I am trying to create a modal view controller after picking an image from the image picker. I use the code :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(#"Picker has returned");
[self dismissModalViewControllerAnimated:YES];
// TODO: make this all threaded?
// crop the image to the bounds provided
img = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(#"orig image size: %#", [[NSValue valueWithCGSize:img.size] description]);
// save the image, only if it's a newly taken image:
if([picker sourceType] == UIImagePickerControllerSourceTypeCamera){
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}
// self.image_View.image=img;
//self.image_View.contentMode = UIViewContentModeScaleAspectFit;
ModalViewController *sampleView = [[ModalViewController alloc] init];
[self presentModalViewController:sampleView animated:YES];
}
However, I receive the warning:
Warning: Attempt to present <ModalViewController: 0x7561600> on <ViewController: 0x75a72e0> while a presentation is in progress!
and the modal view does not appear.
What am I doing wrong?
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
// TODO: make this all threaded?
// crop the image to the bounds provided
img = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(#"orig image size: %#", [[NSValue valueWithCGSize:img.size] description]);
// save the image, only if it's a newly taken image:
if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}
// self.image_View.image = img;
// self.image_View.contentMode = UIViewContentModeScaleAspectFit;
NSLog(#"Picker has returned");
[self dismissViewControllerAnimated:YES
completion:^{
ModalViewController *sampleView = [[ModalViewController alloc] init];
[self presentModalViewController:sampleView animated:YES];
}];
}
Here the issue is happening because, you are first dismissing the UIImagePicker and immediately you are displaying another view as modal view. That's why you are getting this error.
Check with the following code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:NO completion:^{
img = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(#"orig image size: %#", [[NSValue valueWithCGSize:img.size] description]);
if([picker sourceType] == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}
ModalViewController *sampleView = [[ModalViewController alloc] init];
[self presentModalViewController:sampleView animated:YES];
}];
}
I have an universal app that allows to select an image from the device photo library for later manipulation, the code works fine on the iPad but nothing happens on the iPhone, not even the cancel button and after an image is selected nothing happens neither here is my code:
-(IBAction)grabImage:(id)sender
{
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
imgPicker = [[UIImagePickerController alloc] init];
[imgPicker setDelegate:self];
popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[popover setDelegate:self];
CGPoint position = [view1.superview convertPoint:view1.frame.origin toView:nil];
CGRect popOverFrame = CGRectMake( position.x, position.y, self.view.frame.size.width, self.view.frame.size.height );
[popover presentPopoverFromRect:popOverFrame inView:self.view permittedArrowDirections:nil animated:NO];
[popover setPopoverContentSize:CGSizeMake(320, 480)];
[imgPicker release];
}
else
{
imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:self.imgPicker animated:YES];
[imgPicker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
CGImageRef imgRef = pickedImage.CGImage;
app->setImage( pickedImage, CGImageGetWidth(imgRef), CGImageGetHeight(imgRef) );
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
// Enable texture siwth after an image has been loaded
[textureSwitch setEnabled:YES];
[textureSwitch setOn:YES];
app->isTextureDrawingOn = [textureSwitch isOn];
[fillsSwitch setOn:NO];
app->isFillsDrawingOn = [fillsSwitch isOn];
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
[popover dismissPopoverAnimated:YES];
}
ofLog(OF_LOG_VERBOSE, "cancel after selection");
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
[popover dismissPopoverAnimated:YES];
}
ofLog(OF_LOG_VERBOSE, "did cancel");
}
Instead of using below code.
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
Try this code
[self dismissModalViewControllerAnimated:YES];
and also check did you add UIImagePickerControllerDelegate in your interface file.
SOLUTION: (From my comment)
Try this [self.imgPicker dismissModalViewControllerAnimated:YES];
This will work.
For iOS 7: To dismiss a present view controller
[self.imgPicker dismissViewControllerAnimated: YES completion: NULL];
Add UIImagePickerControllerDelegate in your interface file
and then implement this code in your .m file
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
i hope it's solve your problem.
check viewWillAppear or viewDidAppear methods of your parent controller which calls the picker. On iPhone this methods will be called after picker view disappeared. They will not be called after popover disappeared on iPad. I just found error in my code where i set nil to ivar for picked image in viewWillAppear. it take me two days to understand what is happened ;)
Good luck!
An easiest solution:
Add UIImagePickerControllerDelegate in your interface file
and then implement this code in your .m file
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
Swift 3.0
Thanks to MBH this worked for me in my Xcode 8 and iOS 10 project:
internal func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
dismiss(animated: true, completion: nil)
}
For closing it in Swift:
After adding those protocols to you ViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate
internal func imagePickerControllerDidCancel(picker: UIImagePickerController){
dismissViewControllerAnimated(true, completion: nil)
}
I'm able to call the modal view controller for the image picker, pick the image, and crop it, but when I tap 'done' it doesn't do anything, it just hangs with the done button grayed out.
There are no errors, but the function is called.
- (void)viewDidLoad
{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
userImage.image = img;
uploadButton.hidden = NO;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
NSLog(#"called");
}
- (IBAction)getImage:(id)sender {
[self presentModalViewController:self.imgPicker animated:YES];
}
Replace
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
with:
[self dismissModalViewControllerAnimated:YES];
I am using UIimagepickercontroller to browse or take a photo and display it on a subview.
-(IBAction) getPhoto:(id) sender {
secondView1 = [[secondView alloc]
initWithNibName:#"secondView"
bundle:nil];
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:picker animated:YES];
[self.view addSubview:secondView1.view];
}
This works fine for SourceTypeSavedPhotoAlbum, but if I use camera, the secondview1.view does not show. Instead it only shows the original view.
This is my delegate method:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
secondView1.imageView.image = [info
objectForKey:#"UIImagePickerControllerOriginalImage"];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
Thank you for any help!
I finally found out that I should do:
secondView1 = [[secondView alloc]
initWithNibName:#"secondView"
bundle:nil];
self.view addSubview:secondView1.view];
in the delegate function.
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
...
}
Otherwise, the addsubview could be called before the image picker finishes picking an image. Now understand better about how the code flows and the delegate functions.