ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

指针崩溃CGImage崩溃的应用程序

2019-11-27 18:08:54  阅读:277  来源: 互联网

标签:xamarin-ios cgimage ios c pointers


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

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

标签:xamarin-ios,cgimage,ios,c,pointers
来源: https://codeday.me/bug/20191127/2076087.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有