title: Drawing on UIImage / Memory issue - objective-c
body: <p>Greetings,</p>
  
  <p>I'm trying to draw a circle on a map. all the separate pieces of this project work  independently but when I put them all together it breaks.</p>
  
  <p>I setup my UI in my viewDidLoad, retaining most of it.</p>
  
  <p>I then use touch events to call a my refresh map method:</p>
  
  <pre><code>-(void)refreshMap{
  
  NSString *thePath = [NSString stringWithFormat:@"http://maps.google.com/staticmap?center=%f,%f&amp;zoom=%i&amp;size=640x640&amp;maptype=hybrid",viewLatitude, viewLongitude, zoom];
  NSURL *url = [NSURL URLWithString:thePath];
  NSData *data = [NSData dataWithContentsOfURL:url];
  UIImage *mapImage = [[UIImage alloc] initWithData:data];
  
  mapImage = [self addCircle:(mapImage) influence:(70) latCon:(320) lonCon:(320)];
  NSLog(@"-- mapimageview retaincount %i",[mapImage retainCount]);    
  
  mapImageView.image = mapImage;
  [mapImage release];}
  </code></pre>
  
  <p>Setup like this it will load the map with a circle once, but if the map is refreshed again it crashes.</p>
  
  <p>If I comment out the mapImage release it works repeatedly but causes a memory leak.</p>
  
  <p>The addCircle method I'm using:</p>
  
  <pre><code>-(UIImage *)addCircle:(UIImage *)img radius:(CGFloat)radius latCon:(CGFloat)lat lonCon:(CGFloat)lon{
  int w = img.size.width;
  int h = img.size.height; 
  lon = h - lon;
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
  
  //draw the circle
  CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
  CGRect leftOval = {lat- radius/2, lon - radius/2, radius, radius};
  CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 0.3);
  CGContextAddEllipseInRect(context, leftOval);
  CGContextFillPath(context);
  
  CGImageRef imageMasked = CGBitmapContextCreateImage(context);
  CGContextRelease(context);
  CGColorSpaceRelease(colorSpace);
  
  return [UIImage imageWithCGImage:imageMasked];}
  </code></pre>
  
  <p>Any insight/advise is greatly appreciated!</p>