Xamarin.ios &引用;无效句柄“;创建CGBitmapContext
我对CGBitmapcontext有问题。 我在创建带有消息“无效句柄”的CGBitmapContext时出错 这是我的密码:Xamarin.ios &引用;无效句柄“;创建CGBitmapContext,xamarin.ios,cgbitmapcontext,Xamarin.ios,Cgbitmapcontext,我对CGBitmapcontext有问题。 我在创建带有消息“无效句柄”的CGBitmapContext时出错 这是我的密码: var previewContext = new CGBitmapContext(null, (int)ExportedImage.Size.Width, (int)ExportedImage.Size.Height, 8, (int)ExportedImage.Size.Height * 4,
var previewContext = new CGBitmapContext(null, (int)ExportedImage.Size.Width, (int)ExportedImage.Size.Height, 8, (int)ExportedImage.Size.Height * 4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);
谢谢大家, 这是因为要将null传递给第一个参数。CGBitmapContext用于直接绘制到内存缓冲区中。构造函数的所有重载中的第一个参数是(Apple docs): 资料 指向内存中要渲染图形的目标的指针。此内存块的大小应至少为 (字节数*高度)字节 在MonoTouch中,为了方便起见,我们得到了两个接受字节[]的重载。所以你应该这样使用它:
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,也会发生这种情况。谢谢,尽管这种行为不同于apple文档中所说的:data',如果非空,则指向至少
bytesPerRow*height'字节的内存块。如果'data'为空,则上下文的数据将自动分配,并在上下文解除分配时释放。要点!如果您在停止使用上下文之前允许对ctxBuffer进行垃圾收集,您的应用程序将随机崩溃!我在类级别上有上下文变量,而ctxBuffer在创建上下文的函数中是局部变量。在我将ctxBuffer移动到班级级别之前,应用程序一直在崩溃。当心!