title: CGContextFillRects: invalid context - Objective C
body: <p>I have this piece of code to give my images a color I need:</p>
  
  <pre><code>    - (UIImage*)convertToMask: (UIImage *) image
  {
      UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
      CGRect imageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
  
      CGContextRef ctx = UIGraphicsGetCurrentContext();
  
      // Draw a white background (for white mask)
      CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 0.9f);
      CGContextFillRect(ctx, imageRect);
  
      // Apply the source image's alpha
      [image drawInRect:imageRect blendMode:kCGBlendModeDestinationIn alpha:1.0f];
  
      UIImage* outImage = UIGraphicsGetImageFromCurrentImageContext();
  
      UIGraphicsEndImageContext();
  
      return outImage;
  }
  </code></pre>
  
  <p>Everything works great in my first view but when I add this to my detail view, it gives me this error (it still works):</p>
  
  <blockquote>
    <p>CGContextSetRGBFillColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.</p>
    
    <p>CGContextFillRects: invalid context 0x0. This is a serious error. This
    application, or a library it uses, is using an invalid context  and is
    thereby contributing to an overall degradation of system stability and
    reliability. This notice is a courtesy: please fix this problem. It
    will become a fatal error in an upcoming update.</p>
  </blockquote>
  
  <p>Any idea how to get rid of this error?</p>
  
  <p>Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>The action was called with nil for image. I easily fixed it by adding a condition. Thanks @ipmcc for the comment.</p>
  
  <pre><code>    - (UIImage*)convertToMask: (UIImage *) image
  {
  
      if (image != nil) {
  
          UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
          CGRect imageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
  
          CGContextRef ctx = UIGraphicsGetCurrentContext();
  
          // Draw a white background (for white mask)
          CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 0.9f);
          CGContextFillRect(ctx, imageRect);
  
          // Apply the source image's alpha
          [image drawInRect:imageRect blendMode:kCGBlendModeDestinationIn alpha:1.0f];
  
          UIImage* outImage = UIGraphicsGetImageFromCurrentImageContext();
  
          UIGraphicsEndImageContext();
  
          return outImage;
  
      }else{
  
          return image;
  
      }
  }
  </code></pre>