Get pixel color in sceneKit View, iOS, Swift

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 13:23:30

问题


I try to get the color of a specific pixel in my sceneKit view. However, I only get the color of my background. In my scene I have a sphere with an image on the texture. Can somebody help me?

import UIKit

extension UIView {
    func getColourFromPoint(point:CGPoint) -> UIColor {
        let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()!
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)

    var pixelData:[UInt8] = [0, 0, 0, 0]

    let context = CGBitmapContextCreate(&pixelData, 1, 1, 8, 4, colorSpace, bitmapInfo.rawValue)

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

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

    let color:UIColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)
        print(red, green, blue, alpha)
        return color
    }
}

来源:https://stackoverflow.com/questions/36385057/get-pixel-color-in-scenekit-view-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!