Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ImageSource FromStream在Xamarin窗体IOS上不工作_C#_Xamarin_Xamarin.ios_Xamarin.forms_Imagesource - Fatal编程技术网

C# ImageSource FromStream在Xamarin窗体IOS上不工作

C# ImageSource FromStream在Xamarin窗体IOS上不工作,c#,xamarin,xamarin.ios,xamarin.forms,imagesource,C#,Xamarin,Xamarin.ios,Xamarin.forms,Imagesource,我正在从原始像素数据创建位图,然后使用ImageSource.FromStream加载图像。它在android和uwp上工作。我使用以下类手动创建位图 public class BitmapBuilder { private const int BitmapTotalHeaderSize = 54; private const int BitmapHeaderSize = 40; private const int BitCount = 32; private const int Planes

我正在从原始像素数据创建位图,然后使用ImageSource.FromStream加载图像。它在android和uwp上工作。我使用以下类手动创建位图

public class BitmapBuilder
{

private const int BitmapTotalHeaderSize = 54;
private const int BitmapHeaderSize = 40;
private const int BitCount = 32;
private const int Planes = 1;
private const int DPI = 96;
private const int ColorsPerChannel = 4;

private static byte[] s_BitmapHeaderByte;

static BitmapBuilder()
{
    CreateBitmapHeaderByte();
}

private static void CreateBitmapHeaderByte()
{
    s_BitmapHeaderByte = new byte[BitmapTotalHeaderSize];
    s_BitmapHeaderByte[0] = (byte)'B';
    s_BitmapHeaderByte[1] = (byte)'M';
    //Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, buffer, 2, 4);                      // File Size    
    Buffer.BlockCopy(BitConverter.GetBytes(BitmapTotalHeaderSize), 0, s_BitmapHeaderByte, 6, 4);    // Data Offset
    Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, s_BitmapHeaderByte, 10, 4);                  // Padding
    Buffer.BlockCopy(BitConverter.GetBytes(BitmapHeaderSize), 0, s_BitmapHeaderByte, 14, 4);        // Header Size
    //Buffer.BlockCopy(BitConverter.GetBytes((int)width), 0, s_BitmapHeaderByte, 18, 4);            // Width
    //Buffer.BlockCopy(BitConverter.GetBytes((int)height), 0, s_BitmapHeaderByte, 22, 4);           // Height
    Buffer.BlockCopy(BitConverter.GetBytes(Planes), 0, s_BitmapHeaderByte, 26, 2);                  // Planes
    Buffer.BlockCopy(BitConverter.GetBytes(BitCount), 0, s_BitmapHeaderByte, 28, 2);                // Bit count
    Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, s_BitmapHeaderByte, 30, 4);                  // Compression
    Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, s_BitmapHeaderByte, 34, 4);                  // Image Size
    Buffer.BlockCopy(BitConverter.GetBytes(DPI), 0, s_BitmapHeaderByte, 38, 4);                     // XpixelPerM
    Buffer.BlockCopy(BitConverter.GetBytes(DPI), 0, s_BitmapHeaderByte, 42, 4);                     // YpixelPerM
    Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, s_BitmapHeaderByte, 46, 4);                  // Colors Used
    Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, s_BitmapHeaderByte, 50, 4);                  // Colors Important
}

public static byte[] GetBitmapFromRawData(IntPtr pixelData, int width, int height)
{
    byte[] buffer = new byte[height * width * ColorsPerChannel + BitmapTotalHeaderSize];
    Buffer.BlockCopy(s_BitmapHeaderByte, 0, buffer, 0, s_BitmapHeaderByte.Length);
    Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, buffer, 2, 4);    // File Size    
    Buffer.BlockCopy(BitConverter.GetBytes((int)width), 0, buffer, 18, 4);      // Width
    Buffer.BlockCopy(BitConverter.GetBytes((int)height), 0, buffer, 22, 4);     // Height
    Marshal.Copy(pixelData, buffer, BitmapTotalHeaderSize, buffer.Length - BitmapTotalHeaderSize);  // Write raw data
    return buffer;
}
然后我将流设置为图像控件

byte[]buffer=BitmapBuilder.GetBitmapFromRawData(像素数据,(int)宽度,(int)高度);
img.Source=ImageSource.FromStream(()=>newmemoryStream(buffer))

它在IOS上不起作用

然后我保存了手动创建的位图并将其用作文件。它适用于android和UWP,但不适用于IOS

    string sFile = Path.Combine(documentDirectory, "testImage.bmp");

    if (File.Exists(sFile))
    {
        img.Source = ImageSource.FromStream(() => File.OpenRead(sFile));
    }
`


我不明白为什么ImageSource.FromStream似乎不能在IOS上运行。

我们决定使用IOS的依赖服务,并使用CGImage将原始像素数据转换为PNG(而不是位图)。然后,我们从PNG返回流。

我们决定使用iOS依赖服务,并使用CGImage将原始像素数据转换为PNG(而不是位图)。然后我们从PNG返回流。

为什么位图有PDF扩展名?您确定要创建的映像是iOS支持的格式吗?抱歉。复制粘贴错误。在代码示例中修复为testImage.bmp。您确定iOS支持此图像格式吗?您可以直接创建基于PNG/JPEG的CGImage并将其用作UIImage的背景图像,或者使用CoreGraphics从其他支持的格式创建图像,然后将生成的CGImage用作背景图像:(我建议创建一个示例应用程序,将bmp文件直接加载到UIImage中,只是为了验证图像格式不是问题。为什么位图具有PDF扩展名?您确定要创建的图像是iOS支持的格式吗?抱歉。复制粘贴错误。在代码示例中修复为testImage.bmp。您确定此imagiOS支持e格式?您可以直接创建基于PNG/JPEG的CGImage,并将其用作UIImage的背景图像,或使用CoreGraphics从其他支持的格式创建图像,然后将生成的CGImage用作背景图像:(我建议创建一个示例应用程序,将bmp文件直接加载到UIImage中,以验证图像格式是否存在问题