在sceneKit View,iOS,Swift中获取像素颜色

时间:2016-04-03 11:53:10

标签: ios swift colors pixel scenekit

我尝试在sceneKit视图中获取特定像素的颜色。但是,我只得到了背景的颜色。在我的场景中,我有一个带有纹理图像的球体。有人可以帮帮我吗?

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
    }
}

0 个答案:

没有答案