ImageSource FromStream无法处理Xamarin Forms IOS

时间:2017-11-07 16:03:11

标签: c# xamarin xamarin.ios xamarin.forms imagesource

我从原始像素数据创建一个Bitmap,然后使用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;
}

然后我将流设置为Image控件

byte[] buffer = BitmapBuilder.GetBitmapFromRawData(pixelData, (int)width, (int)height); img.Source = ImageSource.FromStream(() => new MemoryStream(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上工作。

1 个答案:

答案 0 :(得分:0)

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