我在Apple的示例代码中注意到,他们经常在CGBitmapContextCreate的bytesPerRow参数中提供0值。例如,这来自反射 http://developer.apple.com/library/ios/#samplecode/Reflection/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008063示例项目。
CGContextRef gradientBitmapContext = CGBitmapContextCreate(NULL, pixelsWide, pixelsHigh,
8, 0, colorSpace, kCGImageAlphaNone);
这对我来说似乎很奇怪,因为我总是采用将图像宽度乘以每个像素的字节数的方法。我尝试将零换入我自己的代码中并进行测试。果然,它仍然有效。
size_t bitsPerComponent = 8;
size_t bytesPerPixel = 4;
size_t bytesPerRow = reflectionWidth * bytesPerPixel;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
reflectionWidth,
reflectionHeight,
bitsPerComponent,
0, // bytesPerRow ??
colorSpace,
kCGImageAlphaPremultipliedLast);
根据文档,bytesPerRow 应该是“位图每行使用的内存字节数”。
那么到底是怎么回事呢?什么时候可以提供零以及什么时候必须计算精确值?以一种或另一种方式执行此操作是否会对性能产生影响?