[英]“Invalid Handle” Create CGBitmapContext
I've got a problem with the CGBitmapcontext. 我的CGBitmapcontext有问题。 I get en error while creating the CGBitmapContext with the message "invalid Handle".
使用消息“无效的句柄”创建CGBitmapContext时出现错误。
Here is my code: 这是我的代码:
var previewContext = new CGBitmapContext(null, (int)ExportedImage.Size.Width, (int)ExportedImage.Size.Height, 8, (int)ExportedImage.Size.Height * 4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);
Thank you; 谢谢;
That is because you are passing null to the first parameter. 那是因为您将null传递给第一个参数。 The CGBitmapContext is for drawing directly into a memory buffer.
CGBitmapContext用于直接绘制到内存缓冲区中。 The first parameter in all the overloads of the constructor is (Apple docs):
构造函数所有重载中的第一个参数是(Apple docs):
data A pointer to the destination in memory where the drawing is to be rendered.
data指向内存中要绘制图形的目标的指针。 The size of this memory block should be at least (bytesPerRow*height) bytes.
该存储块的大小至少应为(bytesPerRow * height)个字节。
In MonoTouch, we get two overloads that accept a byte[] for convenience. 在MonoTouch中,为方便起见,我们得到了两个接受byte []的重载。 So you should use it like this:
因此,您应该像这样使用它:
int bytesPerRow = (int)ExportedImage.Size.Width * 4; // note that bytes per row should
//be based on width, not height.
byte[] ctxBuffer = new byte[bytesPerRow * (int)ExportedImage.Size.Height];
var previewContext =
new CGBitmapContext(ctxBuffer, (int)ExportedImage.Size.Width,
(int)ExportedImage.Size.Height, 8, bytesPerRow, colorSpace, bitmapFlags);
如果传递给方法的width
或height
参数的值为0,也会发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.