【发布时间】:2011-06-23 15:39:02
【问题描述】:
我注意到在 Apple 的示例代码中,他们经常在 CGBitmapContextCreate 的 bytesPerRow 参数中提供值 0。例如,这来自 Reflection 示例项目。
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 应该是“位图每行使用的内存字节数。”
那么交易是什么?我什么时候可以提供零,什么时候必须计算确切的值?以一种或另一种方式执行此操作是否会对性能产生影响?
【问题讨论】:
-
您发布的示例:CGBitmapContextCreate(NULL, pixelWide, pixelHigh, 8,0,colorSpace,kCGImageAlphaNone);无效。你不能在没有 Alpha 通道的情况下创建位图上下文。
-
btw--如果您查看应用程序的日志输出(您可能需要检查 Console.app 中的系统日志)
CGBitmapContextCreate将在您尝试创建位图时打印一条错误消息带有无效参数的上下文。
标签: objective-c ios core-graphics