简体   繁体   中英

How do I get pixel color on touch from inside a SKScene?

I have a spritekit application written in swift and I want to get the color on the pixel that my finger is touching.

I have seen multiple post regarding this and tried them all out but can't seam to get it to work for me. Accourding to other post it should be possible to get the color from a UIView and as a SKScene has a SKIView that inherits from UIView it should be possible to get the color from there.

So to make the question easy and understandable I have an example.

Create a new spritekit application and add a image to it. In my case I created a png image 200x200 pixels with a lot of different colors in it.

This is the GameScene.swift file, it is the only file I have changes from the auto generated:

import SpriteKit

extension UIView {
    func getColorFromPoint(point:CGPoint) -> SKColor {
        var pixelData:[UInt8] = [0,0,0,0]

        let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.toRaw())
        let context = CGBitmapContextCreate(&pixelData, 1, 1, 8, 4, colorSpace, bitmapInfo)

        CGContextTranslateCTM(context, -point.x, -point.y);
        self.layer.renderInContext(context)

        var red:CGFloat = CGFloat(pixelData[0])/CGFloat(255.0)
        var green:CGFloat = CGFloat(pixelData[1])/CGFloat(255.0)
        var blue:CGFloat = CGFloat(pixelData[2])/CGFloat(255.0)
        var alpha:CGFloat = CGFloat(pixelData[3])/CGFloat(255.0)

        var color:SKColor = SKColor(red: red, green: green, blue: blue, alpha: alpha)
        return color
    }
}

class GameScene: SKScene {

    var myColorWheel:SKSpriteNode!

    override func didMoveToView(view: SKView) {
        let recognizerTap = UITapGestureRecognizer(target: self, action:Selector("handleTap:"))
        view.addGestureRecognizer(recognizerTap)

        myColorWheel = SKSpriteNode(imageNamed: "ColorWheel.png")
        myColorWheel.anchorPoint = CGPoint(x: 0, y: 0)
        myColorWheel.position = CGPoint(x: 200, y: 200)
        self.addChild(myColorWheel)
    }

    func handleTap(recognizer : UITapGestureRecognizer)
    {
        let location : CGPoint = self.convertPointFromView(recognizer.locationInView(self.view))
        if(myColorWheel.containsPoint(location))
        {
            let color = self.view?.getColorFromPoint(location)
            println(color)
        }
    }
}

It don't matter where I press on the image on the display, the result is always: Optional(UIDeviceRGBColorSpace 0 0 0 0)

Have you tried to take a snapshot first using:

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates

Then picking the colours from that view?

Not sure how the system renders the .layer in a SKView.

Hope that helps.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM