1

I have a view controller in my project in which I need to click two images and show them in scaled form on the same screen. This process can be repeated after uploading both the images on the server. For taking the picture I am using UIImagePickerController. After clicking the picture 8 times I am receiving a Memory Warning of level 1 and 2.

When I referred related questions on StackOverflow most of them suggested to make the UIImagePickerController as a Singleton object. So I created a new Singleton class which is a subclass of UIImagePickerController. In my main view controller now I am creating an object of this custom class and using it repeatedly. But unfortunately this is also not helping. I am still getting Memory Warning after 8 picture clicks.

Posting some of the snippets also -

Creating a subclass of UIImagePickerController --

@interface ImagePickerControllerSingelton : UIImagePickerController <UIImagePickerControllerDelegate>
{

}
+(ImagePickerControllerSingelton *) sharedPicker;

Implementation of the singleton class --

static ImagePickerControllerSingelton *sharedInstance = nil;

+(ImagePickerControllerSingelton*) sharedPicker
{
   @synchronized (self){
       if (sharedInstance == nil) {
           [[self alloc] init];
           return sharedInstance;
       }
   }
   return sharedInstance;
}

+ (id)allocWithZone:(NSZone *)zone
{
   @synchronized(self) {
       if (sharedInstance == nil) {
           sharedInstance = [super allocWithZone:zone];
           return sharedInstance;  // assignment and return on first allocation

       }
   }

   return nil; //on subsequent allocation attempts return nil
}

Now in the main View Controller class --

ImagePickerControllerSingelton *imagePicker;

imagePicker = [ImagePickerControllerSingelton sharedPicker];
// Set source to the camera
imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;

// Delegate is self
imagePicker.delegate = self;

// Allow editing of image?
imagePicker.allowsEditing = YES;
imagePicker.showsCameraControls = NO;
imagePicker.wantsFullScreenLayout = YES; 

imagePicker.cameraOverlayView = [self createOverlayView]; 
[self presentModalViewController:imagePicker animated:YES];

Am I doing something wrong? Guys please help!

0 Answers0