Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Image from Byte[] in monotouch

I am working in Monotouch framework, in which i have to show image, what i have is byte[].

so i used

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

but it always returns null, i searched for it so came to know that it is a bug in monotouch. bug reported link, so what else function may i use to show image .

like image 318
Saboor Awan Avatar asked Apr 25 '11 15:04

Saboor Awan


1 Answers

Your code is wrong. You are reading from an empty MemoryStream. UIImage.LoadFromData works fine in MonoTouch 4.0 (and since 3.2.* from what I can remember). Try the following method, you don't need a MemoryStream if you already have the byte buffer of the image, eg. from a FileStream:

public static UIImage GetImagefromByteArray (byte[] imageBuffer)
{
    NSData imageData = NSData.FromArray(imageBuffer);
    return UIImage.LoadFromData(imageData);
}
like image 119
Dimitris Tavlikos Avatar answered Nov 01 '22 19:11

Dimitris Tavlikos