简体   繁体   中英

Get/Set pixel color in SKScene, iOS, Swift

I'm trying to write a iOS game dealing with pixels. I found that I can get pixel color with method mentioned here , 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. As no answer for this question

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:

  func getPixelColorAtPoint(point:CGPoint) -> UIColor{

    let pixel = UnsafeMutablePointer<CUnsignedChar>.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
  }

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

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

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.

Recently I try this extension to get pixel from a SKTexture and this work for me:

extension SKTexture {
    func getPixelColorFromTexture(position: CGPoint) -> SKColor {
        let view = SKView(frame: CGRectMake(0, 0, 1, 1))
        let scene = SKScene(size: CGSize(width: 1, height: 1))
        let sprite  = SKSpriteNode(texture: self)
        sprite.anchorPoint = CGPointZero
        sprite.position = CGPoint(x: -floor(position.x), y: -floor(position.y))
        scene.anchorPoint = CGPointZero
        scene.addChild(sprite)
        view.presentScene(scene)
        var pixel: [UInt8] = [0, 0, 0, 0]
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue).rawValue
        let context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, CGColorSpaceCreateDeviceRGB(), bitmapInfo)
        UIGraphicsPushContext(context!)
        view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
        UIGraphicsPopContext()
        return SKColor(red: CGFloat(pixel[0]) / 255.0, green: CGFloat(pixel[1]) / 255.0, blue: CGFloat(pixel[2]) / 255.0, alpha: CGFloat(pixel[3]) / 255.0)
    }
}

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