101

I am using the following code for an image picker. But when I run it in the simulator, I have a memory leak and I get a warning about presentModalViewcontroller:animated being deprecated in iOS6. I also get dismissModalViewController:animated deprecated. I'm using the SDK 6.1.

Code for ImagePicker:

- (void)showAlbum:(id)sender { 
    imagePicker=[[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing =NO;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:imagePicker animated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    //release picker
    [picker dismissModalViewControllerAnimated:YES];
}
Arnaud
  • 7,259
  • 10
  • 50
  • 71
Ram
  • 1,687
  • 3
  • 18
  • 28

5 Answers5

218

Use this line & check:

[self presentViewController:imagePicker animated:YES completion:nil];
Vishal
  • 8,246
  • 6
  • 37
  • 52
  • 1
    In place of this:[self presentModalViewController:imagePicker animated:YES]; – Vishal Apr 08 '13 at 08:03
  • 8
    and for dismiss use this:[self dismissViewControllerAnimated:YES completion:nil]; – Vishal Apr 08 '13 at 08:04
  • Getting same memory leak problem and app will close – Ram Apr 08 '13 at 08:06
  • Where you getting problem means in which line? – Vishal Apr 08 '13 at 08:07
  • Im getting this error 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!' – Ram Apr 08 '13 at 08:14
  • Look this link & follow:http://stackoverflow.com/questions/12690963/preferredinterfaceorientationforpresentation-must-return-a-supported-interface-o – Vishal Apr 08 '13 at 08:15
  • this isn't the modal view controller behavior. A modal view controller should block the calling thread. The calling thread should wait for the modal dialog to close. But this doesn't block – Radu Simionescu Jun 26 '15 at 13:20
17
[[Picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];

Instead of

 [[Picker parentViewControl] dismissModalViewControllerAnimated:YES];

and

[self presentViewController:picker animated:YES completion:nil];

Instead of

[self presentModalViewController:picker animated:YES];
Tirth
  • 7,801
  • 9
  • 55
  • 88
deepesh
  • 171
  • 2
5

As Vishal mentioned

[self presentViewController:imagePicker animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];

make sure you have added "completion:nil" as well

Krishna Sapkota
  • 3,194
  • 2
  • 14
  • 10
4
if ([self respondsToSelector:@selector(presentViewController:animated:completion:)])
{
    [self presentViewController:objSignupViewController animated:^{} completion:nil];
}
else
{
    [self presentModalViewController:objSignupViewController animated:YES];
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Mohit
  • 359
  • 3
  • 5
2

Use:

[self presentViewController:imagePicker animated:YES completion:nil];

And then for your dismissal modal use:

[self dismissViewControllerAnimated:controller completion:nil];

or

[self dismissViewControllerAnimated:YES completion:nil];
halfer
  • 19,824
  • 17
  • 99
  • 186