1
我有點困惑。我有一個ARGB位圖到unsigned char*
數組中,我只是想迭代數組來檢查像素是黑色還是白色。任何人都可以給我發一個這樣的示例代碼嗎?如何迭代ARGB位圖?
爲了得到數組,我使用了這種方法。
CGContextRef CreateARGBBitmapContext (CGSize size) {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
fprintf(stderr, "Error allocating color space\n");
return NULL;
}
void *bitmapData = malloc(size.width * size.height * 4);
if (bitmapData == NULL)
{
fprintf (stderr, "Error: Memory not allocated!");
CGColorSpaceRelease(colorSpace);
return NULL;
}
CGContextRef context = CGBitmapContextCreate (bitmapData, size.width, size.height, 8, size.width * 4, colorSpace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
if (context == NULL)
{
fprintf (stderr, "Error: Context not created!");
free (bitmapData);
return NULL;
}
return context;
}
- (unsigned char *)bitmapFromImage:(UIImage *)image {
//Create a bitmap for the given image.
CGContextRef contex = CreateARGBBitmapContext(image.size);
if (contex == NULL) {
return NULL;
}
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
CGContextDrawImage(contex, rect, image.CGImage);
unsigned char *data = CGBitmapContextGetData(contex);
CGContextRelease(contex);
return data;
}
要測試所有,我使用這個。
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"verticalLine320x460" ofType:@"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
unsigned char *imageBitmap = (unsigned char *)[self bitmapFromImage:image];
[image release];
}
感謝您的閱讀。
謝謝布拉德。你的解決方案工作正常 – 2010-11-01 19:59:52