菜鸟教程小白 发表于 2022-12-11 16:46:42

ios - 在 SKScene、iOS、Swift 中获取/设置像素颜色


                                            <p><p>我正在尝试编写一个处理像素的 iOS 游戏。我发现我可以使用提到的方法获得像素颜色 <a href="https://stackoverflow.com/questions/12770181/how-to-get-the-pixel-color-on-touch/34461183#34461183" rel="noreferrer noopener nofollow">here</a> ,还有其他方法可以获取图像中像素的颜色。但我找不到任何关于在 SKScene 中获取像素颜色的信息。 <a href="https://stackoverflow.com/questions/36385057/get-pixel-color-in-scenekit-view-ios-swift" rel="noreferrer noopener nofollow">As no answer for this question</a> </p>

<p>我认为它与在 UIView 中使用的方法非常相似,但我不熟悉 Swift 中的像素编码。这是我在 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)/255.0, green: CGFloat(pixel)/255.0, blue: CGFloat(pixel)/255.0, alpha: CGFloat(pixel)/255.0)

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

<p>我想我需要更改'''self.layer....'''这一行。谁能帮我解决这个问题?</p>

<p>另外,我找不到任何有关如何直接在 UIView 或 SKScene 中设置像素颜色的信息。有什么想法吗?</p>

<p>顺便说一句:我尝试创建一个大小 = 1 像素的 UIView/SKNode 类,然后将其添加到 UIView/SKScene 以便我可以直接设置颜色。这对于少量像素是可行的。但我正在处理很多,可能有数百或数千个。这不是正确的方法,因为性能很差。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>最近我尝试使用此扩展程序从 <code>SKTexture</code> 获取像素,这对我有用:</p>

<pre><code>extension SKTexture {
    func getPixelColorFromTexture(position: CGPoint) -&gt; 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: =
      let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue).rawValue
      let context = CGBitmapContextCreate(&amp;pixel, 1, 1, 8, 4, CGColorSpaceCreateDeviceRGB(), bitmapInfo)
      UIGraphicsPushContext(context!)
      view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
      UIGraphicsPopContext()
      return SKColor(red: CGFloat(pixel) / 255.0, green: CGFloat(pixel) / 255.0, blue: CGFloat(pixel) / 255.0, alpha: CGFloat(pixel) / 255.0)
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 SKScene、iOS、Swift 中获取/设置像素颜色,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37936036/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37936036/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 SKScene、iOS、Swift 中获取/设置像素颜色