c# - MonoTouch - 来自 Pointer 的 CGImage 使应用程序崩溃 - iPhone

标签 c# ios pointers xamarin.ios cgimage

我有一种方法可以用来创建单个大位图,然后用较小的图 block 图像填充它:

private CGBitmapContext ExtractWriteableBitmap( RGBPaletteRecord rgbPalette, double dpi, ChartIndexFile indexFile, RasterChartFile chartFile )
        {
            //CGBitmapContext bitmapImage = null;
            TileRecord tile;

            // calc the number of tiles in each plane
            int tileCountX = indexFile.TileIndexRecords.Max(ti => ti.X);
            int tileCountY = indexFile.TileIndexRecords.Max(ti => ti.Y);

            // create the big picture
            int pixelWidth = (tileCountX + 1) * TileRecord.PixelWidth;
            int pixelHeight = (tileCountY + 1) * TileRecord.PixelHight;

            int intDPI = Convert.ToInt32(dpi);
            intDPI = 8;

            byte[] sourceArray;
            RectangleF sourceRect;

            // create the big picture 
            CGBitmapContext bitmapImage = new CGBitmapContext(IntPtr.Zero, pixelWidth, pixelHeight, intDPI, pixelWidth * 4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);

            //copy the tiles into the big picture
            int index = 0;
            foreach (TileIndexRecord tileIndexRecord in indexFile.TileIndexRecords)
            {
                // get the tile record
                tile = chartFile.TileRecords[index];

                // extract the byte array for the given palette
                sourceArray = tile.GetBytes(rgbPalette);

                unsafe {
                    IntPtr unmanagedPointer = Marshal.AllocHGlobal(sourceArray.Length);
                    Marshal.Copy(sourceArray, 0, unmanagedPointer, sourceArray.Length);
                    // Call unmanaged code
                    CGImage image = new CGImage(unmanagedPointer);

                    Marshal.FreeHGlobal(unmanagedPointer);
                }

                sourceRect = new RectangleF(0, 0, TileRecord.PixelWidth, TileRecord.PixelHight); 

                bitmapImage.DrawImage(new RectangleF(0,0,128,128),image);

                // copy the tile image into the big picture
                //bitmapImage.WritePixels(sourceRect, sourceArray, TileRecord.Stride, (tileIndexRecord.X * TileRecord.PixelWidth), (tileIndexRecord.Y * TileRecord.PixelHight));

                // increment the index
                index++;
            }
            return bitmapImage;
        }

我遇到的问题是这条线:

CGImage image = new CGImage(unmanagedPointer);

当这条线运行时,我得到以下堆栈跟踪:

at (wrapper managed-to-native) MonoTouch.CoreGraphics.CGImage.CGImageRetain (intptr) <IL 0x00023, 0xffffffff>
  at MonoTouch.CoreGraphics.CGImage..ctor (intptr,bool) [0x00013] in /Developer/MonoTouch/Source/monotouch/src/shared/CoreGraphics/CGImage.cs:104
  at MonoTouch.CoreGraphics.CGImage..ctor (intptr) <IL 0x00003, 0x0002b>
  at Jargoon.Data.Arcs.Loader.ExtractWriteableBitmap (Jargoon.Data.Arcs.Records.RGBPaletteRecord,double,Jargoon.Data.Arcs.Raschts.ChartIndexFile,Jargoon.Data.Arcs.Raschts.RasterChartFile) [0x000db] in /Users/jacknutkins/Desktop/Jargoon/ARCSViewer/ARCSViewer/Loader.cs:587
  at Jargoon.Data.Arcs.Loader.GetHiResImage (Jargoon.Data.Arcs.Records.RGBPaletteRecord) [0x00000] in /Users/jacknutkins/Desktop/Jargoon/ARCSViewer/ARCSViewer/Loader.cs:362
  at ARCSViewer.ARCSViewerViewController.ViewDidLoad () [0x0001c] in /Users/jacknutkins/Desktop/Jargoon/ARCSViewer/ARCSViewer/ARCSViewerViewController.cs:36
  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) <IL 0x0004e, 0xffffffff>
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
  at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
  at ARCSViewer.Application.Main (string[]) [0x00000] in /Users/jacknutkins/Desktop/Jargoon/ARCSViewer/ARCSViewer/Main.cs:17
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

如果我设置断点并逐步执行代码,当我到达问题行时,变量具有以下状态(注意:此错误发生在第一次迭代时):

unmanagedPointer - 0xa25e00 - System.IntPtr
sourceArray - {byte[49152]} - byte[] 

并且在进一步检查变量时,它们似乎被正确填充。

谁能解释一下这是怎么回事?

我是 MonoTouch 开发的新手,但我有 iOS 和 Objective-C 方面的经验,所以如果您需要更多信息,请告诉我。

提前致谢

酪蛋白

最佳答案

CGImage (IntPtr) 构造函数用于为现有的 ObjectiveC CGImage 创建托管包装器,而不是用于从位图数据创建它。

我相信你想做这样的事情:

using (var provider = new CGDataProvider (sourceArray)) {
    using (var image = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitmapFlags, provider, null, false, intent)) {
        bitmapImage.Draw (new RectangleF(0,0,128,128),image);
    }
}

关于c# - MonoTouch - 来自 Pointer 的 CGImage 使应用程序崩溃 - iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12296153/

相关文章:

c# - 生成仅具有选定属性的对象概览

ios - 适用于 iOS5 的 FFmpeg

html - Safari 中的 CSS 3D 变换

objective-c - xcode 导航按钮 url

c# - 如何使用多维数组调用 Parallel.ForEach

c# - 使用 Unity 注入(inject)属性值 - 类型转换

c# - 文件详细信息选项卡中的 "Language neutral"属性

c++ - 将迭代器中的数据插入列表的问题

c# - 在 c# 中使用 c lib - 包含指向数组第一个元素的指针的结构

pointers - 在 Golang 中将接口(interface)设置为 nil