title: UIImage: fill background
body: <p>I've made this below <code>fitImage</code> function which takes an <code>UIImage</code> and a <code>CGSize</code>. In case the input image is larger then the input box, the image would be scaled down to fill into the box. If the box and image does not have the same ratio the image does not fill the box entirely, and there would be some visible background. In this case this background is always white. How can I change this to e.g. a black background?</p>
  
  <pre><code>+ (UIImage*) fitImage:(UIImage*)image inBox:(CGSize)size {
  
      if (image.size.width==size.width &amp;&amp; image.size.height==size.height)
          return image;
  
      if (image.size.width&lt;size.width &amp;&amp; image.size.height&lt;size.height)
          return [Util scaleImage:image toSize:size];
  
      float widthFactor = size.width / image.size.width;
      float heightFactor = size.height / image.size.height;
  
      CGSize scaledSize = size;
      if (widthFactor&lt;heightFactor) {
          scaledSize.width = size.width;
          scaledSize.height = image.size.height * widthFactor;
      } else {
          scaledSize.width = image.size.width * heightFactor;
          scaledSize.height = size.height;
      }
  
      UIGraphicsBeginImageContextWithOptions( size, NO, 0.0 );
  
      float marginX = (size.width-scaledSize.width)/2;
      float marginY = (size.height-scaledSize.height)/2;
      CGRect scaledImageRect = CGRectMake(marginX, marginY, scaledSize.width, scaledSize.height );
  
      [image drawInRect:scaledImageRect];
  
      UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();    
  
      return scaledImage;
  }
  </code></pre>
  
  <p>Please tell me if you need further information.</p>
  
  <p><strong>Solution:</strong></p>
  
  <pre><code>+ (UIImage*) fitImage:(UIImage*)image inBox:(CGSize)size withBackground:(UIColor*)color {
  
      if (image.size.width==size.width &amp;&amp; image.size.height==size.height)
          return image;
  
      if (image.size.width&lt;size.width &amp;&amp; image.size.height&lt;size.height)
          return [Util scaleImage:image toSize:size];
  
      float widthFactor = size.width / image.size.width;
      float heightFactor = size.height / image.size.height;
  
      CGSize scaledSize = size;
      if (widthFactor&lt;heightFactor) {
          scaledSize.width = size.width;
          scaledSize.height = image.size.height * widthFactor;
      } else {
          scaledSize.width = image.size.width * heightFactor;
          scaledSize.height = size.height;
      }
  
      UIGraphicsBeginImageContextWithOptions( size, NO, 0.0 );
  
      float marginX = (size.width-scaledSize.width)/2;
      float marginY = (size.height-scaledSize.height)/2;
      CGRect scaledImageRect = CGRectMake(marginX, marginY, scaledSize.width, scaledSize.height );
  
      UIImage* temp = UIGraphicsGetImageFromCurrentImageContext();
      [color set];
      UIRectFill(CGRectMake(0.0, 0.0, temp.size.width, temp.size.height));
      [image drawInRect:scaledImageRect];
  
      UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();    
  
      return scaledImage;
  }
  </code></pre>