1

.png 画像の各ピクセルの色を取得したい。サイズは 320*480 です。

しかし、'malloc' または 'calloc' を使用してメモリを取得するたびに、NULL しか取得できません。

コードは次のとおりです。

CGImageRef imageRef = image.CGImage;
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));

NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;

CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaNoneSkipLast);
CGColorSpaceRelease(colorSpace);
NSLog(@"width height %d %d", height, width);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

//The values in Variables view is as follows
 rawData    unsigned char * 0x1324b000
   *rawData   unsigned char   '\0'

求めているサイズmがでかいからでしょうか?

小さな値に対して「malloc」と「calloc」の両方でメモリを取得するのに疲れましたが、すべて無駄でした。malloc を使用する場合は、「memset()」も使用しました。しかし、「生データ」に入るのはNULLだけです。

CGImage も nil ではありません。幅と高さをそれぞれ正しい値 320、480 にします。

助けてください。

前もって感謝します。

4

0 に答える 0