简体   繁体   English

如何从SKScene内部获得像素颜色?

[英]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. 我有一个用swift编写的spritekit应用程序,我希望得到手指触摸的像素上的颜色。

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. 再加上其他帖子应该可以从UIView获得颜色,而SKScene有一个继承自UIView的SKIView应该可以从那里获得颜色。

So to make the question easy and understandable I have an example. 因此,为了使问题变得简单易懂,我有一个例子。

Create a new spritekit application and add a image to it. 创建一个新的spritekit应用程序并向其添加图像。 In my case I created a png image 200x200 pixels with a lot of different colors in it. 在我的情况下,我创建了一个200x200像素的png图像,其中包含许多不同的颜色。

This is the GameScene.swift file, it is the only file I have changes from the auto generated: 这是GameScene.swift文件,它是我自动生成的唯一文件:

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) 无论我在显示器上按下图像的位置,结果总是如此: 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. 不确定系统如何在SKView中呈现.layer。

Hope that helps. 希望有所帮助。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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