我有一些名为 originalFrame 的 CGImageRef 充满了数据。使用它,我想做以下事情:
- 使图像灰度化
- 旋转图像
- 裁剪图像
- 将生成的像素数据馈送到外部库
但是,当我这样做时,无论我做什么,像素缓冲区都是完全空的:
// Convert to grayscale, translate, rotate, crop.
CGColorSpaceRef graySpace = CGColorSpaceCreateDeviceGray();
uint8_t *croppedData = (uint8_t *)malloc( sizeof(uint32_t)*CROPPED_WIDTH*CROPPED_HEIGHT );
memset( croppedData, 0, sizeof(uint32_t)*CROPPED_WIDTH*CROPPED_HEIGHT );
context = CGBitmapContextCreate( croppedData, CROPPED_WIDTH, CROPPED_HEIGHT, 8, height*sizeof(uint32_t), graySpace, kCGImageAlphaNoneSkipFirst );
CGContextClipToRect(context, CGRectMake( 0, 0, CROPPED_WIDTH, CROPPED_HEIGHT ) );
CGContextTranslateCTM( context, 0, width );
CGContextRotateCTM( context, -M_PI/2 );
CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), originalFrame );
CGImageRef tempDisplayImage = CGBitmapContextCreateImage( context );
[self.displayLayer performSelectorOnMainThread:@selector(setContents:) withObject:(id)tempDisplayImage waitUntilDone:YES];
CGImageRelease( tempDisplayImage );
CGContextRelease( context );
CGColorSpaceRelease( graySpace );
CGImageRelease( originalFrame );
我知道平移、旋转和裁剪工作正常,因为正确的图像显示在我的 displayLayer 中。尽管上下文正确绘制了图像,但为什么croppedData 缓冲区仍然为空的任何想法?