title: UIGraphicsGetImageFromCurrentImageContext memory leak for scaling image
body: <p>My iPhone application downloads image files from a server, stores it into NSTemporaryDirectory() and then loads the image in the UI asynchronously. Code flow is like this:</p>
  
  <ol>
  <li>Show view with loading activity indicator and run a image downloader in the background.</li>
  <li>Once the image is downloaded, it is written to a file.</li>
  <li>A timer in the loading view keep checking for the availability of file in the temp directory and once available, loads the image from file and adds the image to the UI.</li>
  <li>Before adding the image, it is scaled to required size.</li>
  </ol>
  
  <p>Problem is, I use UIGraphicsGetImageFromCurrentImageContext to scale the image. Looks like the memory used by the image context is not getting cleaned. The app memory just keeps increasing as more files get downloaded.</p>
  
  <p>Some code below:</p>
  
  <p><strong>Code to scale the image:</strong></p>
  
  <pre><code>
  -(UIImage*)scaleToSize:(CGSize)size image:(UIImage *)imageref
  {
   UIGraphicsBeginImageContext(size);
   [imageref drawInRect:CGRectMake(0, 0, size.width, size.height)];
   UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return scaledImage;
  }
  </code></pre>
  
  <p><strong>Loading image from temp directory:</strong></p>
  
  <pre><code>
  -(void)loadImageFromFile: (NSString *) path
  {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   UIImage * imm = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
   [self performSelectorOnMainThread:@selector(insertImage:) withObject:imm waitUntilDone:YES];
   [pool release];
  }
  </code></pre>
  
  <p><strong>Adding image to view (subset of code):</strong></p>
  
  <pre><code>
   self.imageContainer = [[UIImageView alloc] initWithFrame:CGRectMake(0,80,320,250)];
   [self addSubview:self.imageContainer];
   self.imageContainer.image = [self scaleToSize:CGSizeMake(320.0f, 250.0f) image:imm];
   [imageContainer release];
  </code></pre>
  
  <p>What am I missing here ?</p>