我正在尝试将两个图像添加到上下文中,但它不起作用并抛出
GBitmapContextCreateImage:无效的上下文0x0
错误。我使用以下代码:
//some image
CGImageRef image = ...
//some image as well, but masked. Works perfectly.
CGImageRef blurredAndMasked = CGImageCreateWithMask(blurred, mask);
//Both images are fine. Sure.
//Initializing the context and color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(nil, frameSize.width, frameSize.height, 8, 0, colorSpace, kCGBitmapAlphaInfoMask);
//Drawing images into the context
CGContextDrawImage(ctx, CGRectMake(0, 0, frameSize.width, frameSize.height), image);
CGContextDrawImage(ctx, CGRectMake(0, 0, frameSize.width, frameSize.height), blurredAndMasked);
//getting the resulting image
CGImageRef ret = CGBitmapContextCreateImage(ctx);
//releasing the stuff
CGImageRelease(image);
CGImageRelease(blurred);
CGImageRelease(blurredAndMasked);
CGColorSpaceRelease(colorSpace);
CGContextRelease(ctx);
这看起来很好但是生成的图像都是黑色或看起来非常相似:
代码中应该更改什么?提前谢谢!
答案 0 :(得分:1)
kCGBitmapAlphaInfoMask
不是bitmapInfo
的最后一个(CGBitmapContextCreate
)参数的有效值。它是一个掩码(因此名称),您可以使用&
运算符从CGImageAlphaInfo
值中获取CGBitmapInfo
。您永远不会将kCGBitmapAlphaInfoMask
传递给预期为CGBitmapInfo
或CGImageAlphaInfo
的地方。
假设您不需要特定的字节顺序,我相信这是iOS上具有Alpha通道的最高性能像素格式:
CGContextRef ctx = CGBitmapContextCreate(nil,
frameSize.width, frameSize.height, 8, 0, colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
这应该是没有alpha通道的最高性能:
CGContextRef ctx = CGBitmapContextCreate(nil,
frameSize.width, frameSize.height, 8, 0, colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);