在iOS中绘制像素阵列

时间:2014-05-19 09:53:38

标签: ios objective-c graphics grayscale

我有1&数组0表示黑色,0表示白色。我想要最快的方法来绘制/渲染,反转像素颜色,并在iOS中调整绘图的大小。我写了这段代码,但似乎很慢:

for (int yCoordinate = 0; yCoordinate < height; yCoordinate++)
{
for (int xCoordinate = 0; xCoordinate < width; xCoordinate++)
{
int byteIndex = (bytesPerRow * yCoordinate) + xCoordinate * bytesPerPixel;
        int pixel = jbig2_image_get_pixel(imgData, xCoordinate, yCoordinate);
        float pixelRGB[]={0,0,0,0};

        if(pixel==0)
        {
            pixelRGB[0] = backRGB[0];  //255.f;
            pixelRGB[1] = backRGB[1];
            pixelRGB[2] = backRGB[2];
            pixelRGB[3] = backRGB[3];

        }

        else
        {
            pixelRGB[0] =  textRGB[0]; //0.f
            pixelRGB[1] =  textRGB[1];
            pixelRGB[2] =  textRGB[2];
            pixelRGB[3] =  textRGB[3];

        }


//Assigning new color components

rawData[byteIndex] = (unsigned char) pixelRGB[0];
rawData[byteIndex + 1] = (unsigned char) pixelRGB[1];
rawData[byteIndex + 2] = (unsigned char) pixelRGB[2];
        rawData[byteIndex + 3] = (unsigned char) pixelRGB[3];


}
}

CGColorSpaceRef  colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(rawData,
                                      width,
                                      height,
                                      8,
                                      bytesPerRow,
                                      colorSpace,
                                      (CGBitmapInfo)kCGImageAlphaNoneSkipLast);


CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
newUIImage = [UIImage imageWithCGImage:cgImage];

有没有更好的方法,如CGLayer,UIKit等。如果有人为我提供示例代码和更好的&amp;更快的方法

1 个答案:

答案 0 :(得分:0)

我会使用CGBitmapCreate()并创建每像素1位的图像。 (请注意,您必须将字节数组转换为位图,其中字节数组中的每个字节在位图中变为单个位。)然后,您可以直接从1位创建UIImage像素CGImage。要反转它,您可以使用不同的混合模式(可能是差异模式或可能是XOR?)进行绘制。

相关问题