是否可以将位图图像作为数组?

时间:2011-11-23 12:08:11

标签: objective-c

我想按照数组制作一个地图图像。  我认为需要使用石英......

如何制作位图图片?

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}};

1 个答案:

答案 0 :(得分:1)

以下是从cir数组创建CGImageRef(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);