3

I am using UIImagePickerController to select the image from the PhotoLibrary in my application. I have used two different approaches for this. At first I have used a class variouble UIImagePicker with below code.

     imagepicker = [[UIImagePickerController alloc]init];
     imagepicker.delegate = self;
     imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     imagepicker.modalTransitionStyle = UIModalTransitionStylePartialCurl;
     [self presentModalViewController:self.imagepicker animated:YES];

Above code is working fine.But when I clicked on the button it is taking some time to react with the animation in this case.Then I used the autorelease pool approach with this method

    NSAutoreleasePool *pool;
    pool = [[NSAutoreleasePool alloc] init]; 
    if([UIImagePickerController isSourceTypeAvailable:
        UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker= [[[UIImagePickerController alloc]init]autorelease];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.modalTransitionStyle = UIModalTransitionStylePartialCurl;
        [self presentModalViewController:picker animated:YES];

    }
    [pool release];

Also works charm. Both of them showing no leak in the analyser.Can anybody point me the right approach.

Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
angry_developer
  • 215
  • 1
  • 11

1 Answers1

2

Well, no much to say here... Both approaches work, both approaches are correct, use whichever you prefer.

One minor point: if you are regularly presenting the image picker, you better use the first method, and assign it to an instance variable (it isn't called a "class variable"!) only for the first time, and don't release it until - dealloc - this way, you save the continuous allocation-deallocation of the image picker every single time the user chooses an image.

  • @angry_developer Doesn't matter. Both manage the memory correctly. –  Oct 30 '12 at 08:12
  • ut when i click on the button it takes some time to go to the picker? – angry_developer Oct 30 '12 at 08:15
  • @angry_developer apparently it does. And now what? –  Oct 30 '12 at 08:16
  • @angry_developer and `H2CO3` one of the best way could be if you just create that `imagePickerController` as `Singleton` .i think so , but still +1 for same – Kamar Shad Oct 30 '12 at 08:20
  • @Kamarshad Thanks for the upvote, but still: the singleton is not the best pattern for view controllers. See the advice in the 2nd part of my answer :) –  Oct 30 '12 at 08:23
  • @H2CO3 actually one of the problem with ImagePickerController is , it leads some memory leaks on each presentation/dismissal.that's why i just gave that opinion. – Kamar Shad Oct 30 '12 at 08:26
  • @Kamarshad are you sure that `UIImagePickerController` leaks memory? I doubt it. If you are sure, file a bug report to Apple. –  Oct 30 '12 at 08:27
  • @H2CO3 yes i have seen so many thread even on SO for the same. many people have told about that.take a look of it http://stackoverflow.com/questions/6554225/uiimagepickercontroller-memory-leak. – Kamar Shad Oct 30 '12 at 08:30
  • @Kamarshad Thanks for the info, I'll have a look at it. –  Oct 30 '12 at 08:31
  • @H2CO3 you welcome.if you get any thing regarding this, please don't forget to inform me – Kamar Shad Oct 30 '12 at 08:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18774/discussion-between-h2co3-and-kamarshad) –  Oct 30 '12 at 08:33
  • @H2CO3 actually i have some office work. as i gets free , definitely i'll join you.so right now i would sorry.but it great to talk to you – Kamar Shad Oct 30 '12 at 08:35