在单声道显示来自Byte []的图像

时间:2011-04-25 15:36:19

标签: iphone image stream uiimage xamarin.ios

我在Monotouch框架中工作,我必须在其中显示图像,我所拥有的是byte []。

所以我用了

    Public static Public static UIImage GetImagefromByteArray (byte[] byteArrayIn){
        using (MemoryStream memStream = new MemoryStream ())
        {
            byte[] streamBytes = new byte [byteArrayIn.Length];
            memStream.Read( streamBytes, 0, byteArrayIn.Length);
            NSData data = NSData.FromArray( streamBytes );
            return UIImage.LoadFromData( data );
        }              
    }

但它总是返回null,我搜索它,所以才知道它是monotouch中的一个bug。 bug reported link,我可以使用其他功能来显示图像。

1 个答案:

答案 0 :(得分:29)

你的代码错了。您正在读取空的MemoryStream。 UIImage.LoadFromData在MonoTouch 4.0中运行良好(从我能记住的3.2。*开始)。尝试以下方法,如果您已经拥有图像的字节缓冲区,则不需要MemoryStream,例如。来自FileStream:

public static UIImage GetImagefromByteArray (byte[] imageBuffer)
{
    NSData imageData = NSData.FromArray(imageBuffer);
    return UIImage.LoadFromData(imageData);
}