UIImagePicker freezes - objective-c

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];

Related

Received memory warning in camera UIImagePickerController (XCode)

Is there a way to decrease the memory when using the code below for the UIImagePickerController? The strange thing is that the memory is not coming above 26MB but it still shows the received memory warning. After taking a picture the app crashes. Memory warning only shows when choosing the camera, when choosing the library it's just fine.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==actionSheet.cancelButtonIndex){
return;
}
UIImagePickerControllerSourceType type = UIImagePickerControllerSourceTypePhotoLibrary;
if([UIImagePickerController isSourceTypeAvailable:type]){
if(buttonIndex==0 && [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
type = UIImagePickerControllerSourceTypeCamera;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.delegate = self;
picker.sourceType = type;
[self presentViewController:picker animated:YES completion:nil];
}
}
#pragma mark - Image Picker Controller delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
CLImageEditor *editor = [[CLImageEditor alloc] initWithImage:image];
editor.delegate = self;
[picker pushViewController:editor animated:YES];
}

Add A UIButton In A PopOverController - Xcode

I have an app which has a toolbar at the bottom with one of the options as Upload. On clicking on it, a PopOverController occurs which displays the list of photos to choose from.
After choosing a photo, I want to display a view (inside the PopOverController itself)which contains the photo alongwith the button called as Upload. On choosing my photo, currently my PopOverController is not getting dismissed. I have used the following lines of code :
-(IBAction)photolibrarypressed:(id)sender{
NSLog(#"hi");
UIImagePickerController *picker= [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
self.popoverController = popover;
popoverController.delegate = self;
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
imageView = [[UIImageView alloc] initWithFrame:[window bounds]];
[window addSubview:imageView];
imageView.hidden = YES;
[window makeKeyAndVisible];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
imageView.image = image;
NSLog(#"hellow");
[self dismissModalViewControllerAnimated:YES];
// need to show the upload image button now
upload.hidden = NO;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
exit(0);
}
Can Someone help me to sort out the issue ??
Instead of calling
[self dismissModalViewControllerAnimated:YES];
call the same cancel method as int eh cancel method to handle dismissing it's modal view
[picker dismissModalViewControllerAnimated:YES]; // Dismisses modalView

Problems with UIImagePickerController cancel button not working

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)
}

iOS: UIImagePickerController and ResignFirstResponder

I have an application that allows the user to select a photo from their camera roll, and display it in a UIImageView. But, after they tap on which image they would like to display, the camera roll's view does not disappear. So, I figured that I would need to simply call [sender resignFirstResponder];. But, this did not do the trick. I've been trying multiple things and doing searching around, but to no avail. I'm very new to Objective-C, so any help is much appreciated. Here is the code I'm working with:
(imgPicker is the UIImagePickerController.)
- (IBAction)grabImage(id)sender {
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
_popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[_popover presentPopoverFromRect:self.imageView.bounds inView:self.imageView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else {
[self presentModalViewController:imgPicker animated:YES];
}
[self.imgPicker resignFirstResponder];
}
And this may or may not be relevant to the issue:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
if (imageView.image == nil) {
imageView.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
return;
}
if (imageView2.image == nil) {
imageView2.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
return;
}
}
You need just this line to dismiss modal picker in your delegate method:
[picker dismissModalViewControllerAnimated:YES];
resignFirstResponder in this case is useless
Try
[self dismissModalViewControllerAnimated:YES];
As you want to dismiss only picker view and not parent view of picker
you just have to use:
[picker dismissModalViewControllerAnimated:YES];
and you are done.

UIimagepickercontroller

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.