MonoTouch中的CGBitmapContext抛出无效的句柄异常 [英] CGBitmapContext in MonoTouch throws invalid handle exception

查看:97
本文介绍了MonoTouch中的CGBitmapContext抛出无效的句柄异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MonoTouch的新手,最近我一直在将代码从标准C#转换为与MonoTouch兼容的代码,以便可以在iPhone应用程序中使用.我已经设法转换了代码,但是我遇到了这种方法的问题,该方法用于返回位图上下文:

I'm new to MonoTouch and I've recently been converting code from standard C# to MonoTouch compliant code so it can be used within an iPhone application. I've managed to convert the code, however I've run into a problem with this method which I use to return a bitmap context:

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);

            int bytesPerRow = (int)TileRecord.PixelWidth * 4; // note that bytes per row should 
    //be based on width, not height.

            CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
            CGImageAlphaInfo info = CGImageAlphaInfo.NoneSkipFirst;

            // create the big picture 
            bitmapImage = new CGBitmapContext(System.IntPtr.Zero, pixelWidth, pixelHeight, intDPI, intDPI, colorSpace, info);

            byte[] sourceArray;
            RectangleF sourceRect;

            //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);

                GCHandle pinnedArray = GCHandle.Alloc(sourceArray, GCHandleType.Pinned);
                IntPtr pointer = pinnedArray.AddrOfPinnedObject();
                //do your stuff
                //pinnedArray.Free();

                CGImage image = new CGImage(pointer);
                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;
        }

这是堆栈跟踪:

System.Exception: Invalid handle
  at MonoTouch.CoreGraphics.CGContext..ctor (IntPtr handle, Boolean owns) [0x00022] in /Developer/MonoTouch/Source/monotouch/src/shared/CoreGraphics/CGContext.cs:136
  at MonoTouch.CoreGraphics.CGBitmapContext..ctor (IntPtr data, Int32 width, Int32 height, Int32 bitsPerComponent, Int32 bytesPerRow, MonoTouch.CoreGraphics.CGColorSpace colorSpace, CGImageAlphaInfo bitmapInfo) [0x00000] in <filename unknown>:0
  at Jargoon.Data.Arcs.Loader.ExtractWriteableBitmap (Jargoon.Data.Arcs.Records.RGBPaletteRecord rgbPalette, Double dpi, Jargoon.Data.Arcs.Raschts.ChartIndexFile indexFile, Jargoon.Data.Arcs.Raschts.RasterChartFile chartFile) [0x00079] in /Users/jacknutkins/Desktop/Jargoon/ARCSViewer/ARCSViewer/Loader.cs:569
  at Jargoon.Data.Arcs.Loader.GetHiResImage (Jargoon.Data.Arcs.Records.RGBPaletteRecord rgbPalette) [0x00000] in /Users/jacknutkins/Desktop/Jargoon/ARCSViewer/ARCSViewer/Loader.cs:361
  at ARCSViewer.ARCSViewerViewController.ViewDidLoad () [0x0001c] in /Users/jacknutkins/Desktop/Jargoon/ARCSViewer/ARCSViewer/ARCSViewerViewController.cs:37
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29
  at ARCSViewer.Application.Main (System.String[] args) [0x00000] in /Users/jacknutkins/Desktop/Jargoon/ARCSViewer/ARCSViewer/Main.cs:17

该错误发生在以下行:

// create the big picture 
            bitmapImage = new CGBitmapContext(System.IntPtr.Zero, pixelWidth, pixelHeight, intDPI, intDPI, colorSpace, info);

我对MonoTouch还是很陌生,所以如果它非常明显,请尝试和我一起忍受,但是谁能解释这是怎么回事?

I'm very new to MonoTouch so try and bear with me if its glaringly obvious but can anyone explain whats going on?

推荐答案

通常在内存不足时发生. (设备)控制台通常会打印更多信息.

This usually happens when you're running out of memory. The (device) console will usually print more information though.

我的猜测是您没有释放正在创建的CGBitmapContext.

My guess is that you're not freeing the CGBitmapContext you're creating.

这篇关于MonoTouch中的CGBitmapContext抛出无效的句柄异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆