【问题标题】:Draw Pixel Array in iOS在 iOS 中绘制像素数组
【发布时间】:2014-05-20 12:23:06
【问题描述】:

我有 1 和 0 的数组。1 代表黑色,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 等。如果有人提供示例代码以及更好更快的方法,我将不胜感激

【问题讨论】:

    标签: ios objective-c graphics grayscale


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 2013-10-05
      • 1970-01-01
      相关资源
      最近更新 更多