2011-11-23 207 views
1

我想做一個位圖圖像作爲下面的數組。 我認爲需要使用石英...是否有可能將位圖圖像製作爲數組?

如何製作位圖圖像?

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

這裏是代碼從C陣列創建CGImageRef(cir)。請注意,事情可以更簡單,如果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); 
+0

哦〜太好了。 :D –

+0

謝謝你的幫助。 –

相關問題