简体   繁体   English

ImageSource FromStream在Xamarin Forms IOS上不起作用

[英]ImageSource FromStream not working on Xamarin Forms IOS

I am creating a Bitmap from raw pixel data and then using ImageSource.FromStream to load the image. 我正在从原始像素数据创建位图,然后使用ImageSource.FromStream加载图像。 It works on android and uwp. 它适用于android和uwp。 I manually create the bitmap with the following class. 我使用以下类手动创建位图。

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

Then I set the stream to the Image control 然后我将流设置为Image控件

byte[] buffer = BitmapBuilder.GetBitmapFromRawData(pixelData, (int)width, (int)height); img.Source = ImageSource.FromStream(() => new MemoryStream(buffer));

It does not work on IOS. 它不适用于IOS。

I then saved the manually created bitmap and used it as a file. 然后,我保存了手动创建的位图并将其用作文件。 It works in android and UWP but not IOS. 它适用于android和UWP,但不适用于IOS。

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

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

` `

I am at a loss as why ImageSource.FromStream does not appear to be working on IOS. 我很茫然,为什么ImageSource.FromStream似乎无法在IOS上运行。

We have decided to use the Dependency Service for iOS and use a CGImage for converting raw pixel data to a PNG (instead of a bitmap). 我们已决定使用iOS的依赖项服务,并使用CGImage将原始像素数据转换为PNG(而不是位图)。 We then return the stream from the PNG. 然后,我们从PNG返回流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM