Вот код для создания CGImageRef (cir
) из массива C
.Обратите внимание, что все может быть проще, если массив C
является одномерным со значениями 0 и 255.
size_t width = 10;
size_t height = 10;
int bit_map[10][10] = {
{0,0,1,0,0,0,0,1,0,0},
{0,1,1,1,1,0,1,1,1,0},
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{0,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,1,1,0},
{0,0,1,1,1,1,1,1,0,0},
{0,0,0,1,1,1,1,0,0,0},
{0,0,0,0,1,1,0,0,0,0}
};
UIImage *barCodeImage = nil;
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 8;
size_t bytesPerRow = (width * bitsPerPixel + 7) / 8;
void *imageBytes;
size_t imageBytesSize = height * bytesPerRow;
imageBytes = calloc(1, imageBytesSize);
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
int pixel = bit_map[j][i];
if (pixel == 1)
pixel = 255;
((unsigned char*)imageBytes)[((i+1) * (j+1)) - 1] = pixel;
}
}
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imageBytes, imageBytesSize, releasePixels);
CGColorSpaceRef colorSpaceGrey = CGColorSpaceCreateDeviceGray();
CGImageRef cir = CGImageCreate (width,
height,
bitsPerComponent,
bitsPerPixel,
imageBytesSize,
colorSpaceGrey,
kCGImageAlphaNone,
provider,
NULL,
NO,
kCGRenderingIntentDefault);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceGrey);