title: How to release CGImageRef if required to return it?
body: <p>I have a method to resize a <code>CGImageRef</code> and return <code>CGImageRef</code>. The issue is the last few lines, where I need to somehow release but return it after. Any ideas? Thanks</p>
  
  <pre><code> -(CGImageRef)resizeImage:(CGImageRef *)anImage width:(CGFloat)width height:(CGFloat)height
  {
  
      CGImageRef imageRef = *anImage;
  
      CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
  
      if (alphaInfo == kCGImageAlphaNone)
          alphaInfo = kCGImageAlphaNoneSkipLast;
  
  
      CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
  
      CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
  
      CGImageRef ref = CGBitmapContextCreateImage(bitmap);
  
      CGContextRelease(bitmap);
      CGImageRelease(ref); //issue here
  
      return ref;
  
  }
  </code></pre>