title: Received memory warning then 'Lost connection to iPhone'.
body: <p>I use the below code snippet to open the camera, click a photo and use the photo (when the two options, <em>Retake</em> and <em>Use Photo</em>, appear). </p>
  
  <pre><code>- (IBAction)openCamera
  {
      @autoreleasepool {
          [imageSpinner stopAnimating];
          [imageSpinner removeFromSuperview];
          [cameralabel setText:@""];
          if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
          {
              UIImagePickerController *imagePicker =
              [[UIImagePickerController alloc] init];
              imagePicker.delegate = self;
              imagePicker.sourceType =
              UIImagePickerControllerSourceTypeCamera;
              imagePicker.allowsEditing = NO;
              [self presentViewController:imagePicker animated:NO completion:nil];
              imagePicker=nil;
          }
      }
  }
  
  -(void)setImageSpinner
  {
      @autoreleasepool {
          imageSpinner = nil;
          imageSpinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
          imageSpinner.center = self.view.center;
          [imageSpinner startAnimating];
          [self.view addSubview:imageSpinner];
          [self.view bringSubviewToFront:imageSpinner];
      }
  }
  
  - (void)viewDidLoad
  {
      [self openCamera];
      [super viewDidLoad];
  }
  
  - (void)viewDidAppear:(BOOL)animated
  {
      [super viewDidAppear:animated];
  }
  
  -(void)viewWillAppear:(BOOL)animated
  {
      [super viewWillAppear:animated];
      [self.navigationController setToolbarHidden:YES];
      self.navigationItem.hidesBackButton = NO;
  }
  
  -(void)viewWillDisappear:(BOOL)animated
  {
      [super viewWillDisappear:animated];
  }
  
  -(void)viewDidDisappear:(BOOL)animated
  {
      [super viewDidDisappear:animated];
  }
  
  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
      NSString *savedImagePath;
      @autoreleasepool {
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
         savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"Image 1.jpeg"];
          paths=nil;
          documentsDirectory=nil;
          [picker dismissViewControllerAnimated:NO completion:NULL];
          [cameralabel setText:@"Please wait"];
          [self setImageSpinner];
      }
      @autoreleasepool {
          ManifestDocumentViewController *manifestVC = [self.storyboard instantiateViewControllerWithIdentifier:@"manifestImage"];
          manifestVC.getTempFilePath = savedImagePath;
         if ([screenName isEqualToString:@"Retake"])
          {
              manifestVC.screenName = @"Retake";
              manifestVC.retakeFileID = retakeFileID;
              manifestVC.retakeFilePath = retakeFilePath;
              NSData *data = [NSData dataWithContentsOfFile:savedImagePath];
              [data writeToFile:retakeFilePath atomically:YES];
              data=nil;
          }
          else
          {
              manifestVC.screenName = @"Camera";
              UIImage *image = info[UIImagePickerControllerOriginalImage];
             NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
              [imageData writeToFile:savedImagePath atomically:NO];
              image=nil;
              imageData=nil;
          }
          savedImagePath=nil;
         [self.navigationController pushViewController:manifestVC animated:YES];
      }
  }
  
  - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  {
      [picker dismissViewControllerAnimated:YES completion:NULL];
      picker.delegate=nil;
      picker=nil;
  }
  
  - (UIStatusBarStyle)preferredStatusBarStyle
  {
      return UIStatusBarStyleLightContent;
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      [[NSURLCache sharedURLCache] removeAllCachedResponses];
  }
  </code></pre>
  
  <p>During a debugging session on iPhone 4s, the memory spikes up as shown by the memory usage under the Debug navigator.  Attached along side is a screenshot of the Memory Usage comparison. The raise in 'Other Processes' is a matter of concern. </p>
  
  <p><a href="https://i.stack.imgur.com/GDZLs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GDZLs.png" alt="Memory Usage, App Debugging"></a></p>
  
  <p><em>Received memory warning</em> also appears among the displayed logs. After running the code a number of times the app crashes (The message 'Lost connection to iPhone'). This corresponds to a spike in the memory and the 'Free' memory being consumed completely.</p>
  
  <p>Can the above code be optimised as far as possible to ensure that the memory usage gets reduced as much as possible.</p>