title: Swift color detection in center of screen
body: <p>I wants to find the pixel colors in the middle of the screen.</p>
  
  <pre><code>extension UIImage {
  
  func getPixelColor(atLocation location: CGPoint, withFrameSize size: CGSize) -&gt; UIColor {
      let pixelPlace: CGPoint = CGPoint(x: location.x, y: location.y)
  
      let pixelData = self.cgImage!.dataProvider!.data
      let data: UnsafePointer&lt;UInt8&gt; = CFDataGetBytePtr(pixelData)
  
      let pixelIndex: Int = ((Int(self.size.width) * Int(pixelPlace.y)) + Int(pixelPlace.x)) * 4
  
      let r = CGFloat(data[pixelIndex])// / CGFloat(255.0)
      let g = CGFloat(data[pixelIndex+1])// / CGFloat(255.0)
      let b = CGFloat(data[pixelIndex+2])// / CGFloat(255.0)
      let a = CGFloat(data[pixelIndex+3])// / CGFloat(255.0)
  
      return UIColor(red: r, green: g, blue: b, alpha: a)
  }
  }
  </code></pre>
  
  <p>Then I print the color with this.</p>
  
  <pre><code> let place = CGPoint(x: self.view.center.x , y: self.view.center.y)
      print(yourImageView.image!.getPixelColor(atLocation: place, withFrameSize: self.view.frame.size))
  </code></pre>
  
  <p>How do I get the center pixel's color? I keep getting other color values than what is in the center. The color detection is functional so I don't believe the problem comes from that.</p>
  
  <p>What I changed (same problem still)</p>
  
  <pre><code>extension UIView {
  func getColor(at point: CGPoint) -&gt; UIColor{
  
      let pixel = UnsafeMutablePointer&lt;CUnsignedChar&gt;.allocate(capacity: 4)
      let colorSpace = CGColorSpaceCreateDeviceRGB()
      let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
      let context = CGContext(data: pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!
  
      context.translateBy(x: -point.x, y: -point.y)
      self.layer.render(in: context)
      let color = UIColor(red:   CGFloat(pixel[0]),
                          green: CGFloat(pixel[1]),
                          blue:  CGFloat(pixel[2]),
                          alpha: CGFloat(pixel[3]))
  
      pixel.deallocate(capacity: 4)
      return color
  }
  }
  </code></pre>
  
  <p>And I access it this way</p>
  
  <pre><code>         let loc = CGPoint(x: yourImageView.center.x, y: yourImageView.center.y)
      var color = yourImageView.getColor(at: loc)
      print(color)
  </code></pre>