Get/Set pixel color in SKScene, iOS, Swift
<p>I'm trying to write a iOS game dealing with pixels. I found that I can get pixel color with method mentioned <a href="https://stackoverflow.com/questions/12770181/how-to-get-the-pixel-color-on-touch/34461183#34461183">here</a>, and there other method for getting color of pixel in images too. But I could not find any information on getting color of pixel in SKScene. <a href="https://stackoverflow.com/questions/36385057/get-pixel-color-in-scenekit-view-ios-swift">As no answer for this question</a></p>

<p>I image it would be very similar to the method used to do it in UIView, but I'm not familiar with pixel coding in Swift. Here is what I used for getting pixel color in UIView:</p>

<pre><code>  func getPixelColorAtPoint(point:CGPoint) -&gt; UIColor{

    let pixel = UnsafeMutablePointer&lt;CUnsignedChar&gt;.alloc(4)
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
    let context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, bitmapInfo.rawValue)

    CGContextTranslateCTM(context, -point.x, -point.y)
    self.layer.renderInContext(context!)
    let color:UIColor = UIColor(red: CGFloat(pixel[0])/255.0, green: CGFloat(pixel[1])/255.0, blue: CGFloat(pixel[2])/255.0, alpha: CGFloat(pixel[3])/255.0)

    pixel.dealloc(4)
    return color
  }
</code></pre>

<p>I guess I need to change the line '''self.layer....'''. Could anyone help me with this?</p>

<p>Also, I could not find any information about how to set a pixel color directly in UIView or SKScene. Any ideas?</p>

<p>BTW: I tried to create a UIView/SKNode class with size = 1 pixel, then add it to the UIView/SKScene so I can set the color directly. This is doable for small amount of pixels. But I'm dealing with lots of them, probably hundreds or thousands. This is not the right way to do it since the performance is very poor.</p>