"无效句柄"创建CGBitmapContext

Ale*_*lex 2 cgbitmapcontext xamarin.ios

我遇到了CGBitmapcontext的问题.我在创建带有"无效句柄"消息的CGBitmapContext时遇到错误.

这是我的代码:

var previewContext = new CGBitmapContext(null, (int)ExportedImage.Size.Width, (int)ExportedImage.Size.Height, 8, (int)ExportedImage.Size.Height * 4,                                                    CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);
Run Code Online (Sandbox Code Playgroud)

谢谢;

Dim*_*kos 6

那是因为您将null传递给第一个参数.CGBitmapContext用于直接绘制到内存缓冲区中.构造函数的所有重载中的第一个参数是(Apple docs):

data 指向要在其中呈现图形的内存中的目标的指针.此内存块的大小应至少为(bytesPerRow*height)字节.

在MonoTouch中,为方便起见,我们得到两个接受byte []的重载.所以你应该像这样使用它:

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);
Run Code Online (Sandbox Code Playgroud)