title: Get average color of UIImage in Swift
body: <p>I was recently attempting to convert the code from <a href="http://www.bobbygeorgescu.com/2011/08/finding-average-color-of-uiimage/" rel="noreferrer">here</a> into Swift. However, I keep getting a white color, no matter the image. Here's my code:</p>
  
  <pre><code>// Playground - noun: a place where people can play
  
  import UIKit
  
  extension UIImage {
      func averageColor() -&gt; UIColor {
          var colorSpace = CGColorSpaceCreateDeviceRGB()
          var rgba: [CGFloat] = [0,0,0,0]
          var context = CGBitmapContextCreate(&amp;rgba, 1, 1, 8, 4, colorSpace, CGBitmapInfo.fromRaw(CGImageAlphaInfo.PremultipliedLast.toRaw())!)
          rgba
  
          CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), self.CGImage)
  
          if rgba[3] &gt; 0 {
              var alpha = rgba[3] / 255
              var multiplier = alpha / 255
              return UIColor(red: rgba[0] * multiplier, green: rgba[1] * multiplier, blue: rgba[2] * multiplier, alpha: alpha)
          } else {
              return UIColor(red: rgba[0] / 255, green: rgba[1] / 255, blue: rgba[2] / 255, alpha: rgba[3] / 255)
          }
      }
  }
  
  var img = UIImage(data: NSData(contentsOfURL: NSURL(string: "http://upload.wikimedia.org/wikipedia/commons/c/c3/Aurora_as_seen_by_IMAGE.PNG")))
  
  img.averageColor()
  </code></pre>
  
  <p>Thanks in advance.</p>