title: Why does my CGImage not display correctly after setNeedsDisplay
body: <p>I'm drawing a colorWheel in my app. I do this by generating a CGImage in a separate function and using CGContextDrawImage in drawRect to put it onscreen.</p>
  
  <p>On the initial presentation it looks fine, but after I call setNeedsDisplay (or setNeedsDisplayInRect) the image turns black/distorted. I must be doing something stupid, but can't see what.</p>
  
  <p><img src="https://i.imgur.com/TiZDuaw.png" alt="Cool Wheel">
  <img src="https://i.imgur.com/jrQDtqn.png" alt="Cool Wheel"></p>
  
  <p>DrawRect code looks like:</p>
  
  <pre><code>override func drawRect(rect: CGRect) {
  
      if let context = UIGraphicsGetCurrentContext() {
  
          let wheelFrame = CGRectMake(0,0,circleRadius*2,circleRadius*2)
          CGContextSaveGState(context)
  
              //create clipping mask for circular wheel
          CGContextAddEllipseInRect(context, wheelFrame)
          CGContextClip(context)
  
              //draw the wheel
          if colorWheelImage != nil { CGContextDrawImage(context, CGRectMake(0,0,circleRadius*2,circleRadius*2), colorWheelImage) }
          CGContextRestoreGState(context)
  
              //draw a selector element
          self.drawSliderElement(context)
      }
  }
  </code></pre>
  
  <p>CGimage generation function:</p>
  
  <pre><code>func generateColorWheelImage() {
  
      let width       = Int(circleRadius*2)
      let height      = Int(circleRadius*2)
  
      var pixels = [PixelData]()
  
      for yIndex in 0..&lt;width {
          for xIndex in 0..&lt;height {
              pixels.append(colorAtPoint(CGPoint(x: xIndex, y: yIndex)))
          }
      }
  
      let rgbColorSpace   = CGColorSpaceCreateDeviceRGB()
      let bitmapInfo      = CGBitmapInfo.ByteOrderDefault
  
      let bitsPerComponent: Int   = 8
      let bitsPerPixel: Int       = 24
  
      let renderingIntent = CGColorRenderingIntent.RenderingIntentDefault
  
      assert(pixels.count == Int(width * height))
  
      var data = pixels
  
      let providerRef = CGDataProviderCreateWithData(nil, data, data.count * sizeof(PixelData), nil)
  
      colorWheelImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, width * Int(sizeof(PixelData)), rgbColorSpace, bitmapInfo, providerRef, nil, true,renderingIntent)
  
  }
  </code></pre>