2

ここでピクセルの色を取得する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'

上記のコードを調整して、コンテキスト関数のパラメーターを正しく入力する方法を教えていただければ幸いです。

4

1 に答える 1