我在这里找到了一个获取像素颜色的 Objective-C 代码示例: How to get the pixel color on touch?
我需要帮助的特定代码部分是使用 CGColorSpaceCreateDeviceRGB 创建上下文的位置:
---这是Objective-C代码
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel,
1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
我最好的尝试如下(我没有返回任何东西,但我正在尝试首先正确获取上下文):
---这是我最好的 Swift 转换尝试
func getPixelColorAtPoint()
{
let pixel = UnsafeMutablePointer<CUnsignedChar>.alloc(1)
var colorSpace:CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()
let context = CGBitmapContextCreate(pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: nil, bitmapInfo: CGImageAlphaInfo.PremultipliedLast)
}
然而,这给了我一个错误
Cannot convert the expression's type '(UnsafeMutablePointer<CUnsignedChar>, width: IntegerLiteralConvertible, height: IntegerLiteralConvertible, bitsPerComponent: IntegerLiteralConvertible, bytesPerRow: IntegerLiteralConvertible, space: NilLiteralConvertible, bitmapInfo: CGImageAlphaInfo)' to type 'IntegerLiteralConvertible'
如果您能建议我如何调整上面的代码以正确输入上下文函数参数,我将不胜感激,谢谢!