【发布时间】: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