将对象转换为字节数组

问题描述 投票:0回答:1

编辑:

回复晚了非常抱歉。

我在答案中尝试了代码,但是我遇到了从Byte到Image的转换问题。

 public override void ViewDidLoad()
        {

            var image = UIImage.FromFile("image.png");

            var imageWidth = image.Size.Width;
            var imageHeight = image.Size.Height;

            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            var fullfilename = Path.Combine(documents, "Image.json");

            // save json methode

            SaveImageJson(image, fullfilename);

            // read json methode

            var newImage = readImageJson(fullfilename, imageWidth, imageHeight);


        }


        public static void SaveImageJson(UIImage image, string fullfilename)
        {
            // Convert image to byteArray

            Byte[] imageByteArray = ReadFully(image.AsPNG().AsStream());


            // Serialize object
            var json = JsonConvert.SerializeObject(imageByteArray, Formatting.Indented);

            // Save to file

            File.WriteAllText(fullfilename, json);
        }

        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

        public static UIImage readImageJson(string fullfilename, nfloat width, nfloat height)
        {

            var filenameNewImage = File.ReadAllText(fullfilename, System.Text.Encoding.ASCII);

            var jsonNewImage = JsonConvert.DeserializeObject(filenameNewImage);

            Byte[] ByteNewImage = ObjectToByteArray(jsonNewImage);


            UIImage NewImage = new UIImage();

            NewImage = ImageFromBytes(ByteNewImage, width, height);

            return NewImage;

        }


        public static byte[] ObjectToByteArray(Object obj)
        {
            BinaryFormatter bf = new BinaryFormatter();
            using (var ms = new MemoryStream())
            {
                bf.Serialize(ms, obj);
                return ms.ToArray();
            }
        }

        private static UIImage ImageFromBytes(byte[] bytes, nfloat width, nfloat height)
        {
            try
            {
                NSData data = NSData.FromArray(bytes);
                UIImage image = UIImage.LoadFromData(data);
                CGSize scaleSize = new CGSize(width, height);
                UIGraphics.BeginImageContextWithOptions(scaleSize, false, 0);
                image.Draw(new CGRect(0, 0, scaleSize.Width, scaleSize.Height));
                UIImage resizedImage = UIGraphics.GetImageFromCurrentImageContext();
                UIGraphics.EndImageContext();
                return resizedImage;
            }
            catch (Exception)
            {
                return null;
            }
        }

我在将对象转换为字节数组时遇到问题

我有一个Xamarin.IOS模拟,其中我必须读取数据,这是我在使用Json方法之前保存的图像。我在回读数据时遇到问题,我认为它与转换有关系。我使用一种方法将我从Json文件获取的对象转换为字节数组,所以我可以将其转换为UIImage,遗憾的是转换不起作用,因为当我比较保存的字节数组和我保存的字节数据时从方法阅读中得出,我发现它们是不同的

    public override void ViewDidLoad()
    {
        base.ViewDidLoad(); 
  // getting the file that we saved the data before

      var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var filename = Path.Combine(documents, "account.json");
        File.WriteAllText(filename, json);
    var filename2 = File.ReadAllText(filename,System.Text.Encoding.ASCII);

        var json2 = ObjectToByteArray(JsonConvert.DeserializeObject(filename2));


        NSData data2 = NSData.FromArray(json2);

        UIImage image2 = new UIImage();
        image2 = UIImage.LoadFromData(data2);

        img.Image = image2;

    }

    public static byte[] ObjectToByteArray(Object obj)
    {
        BinaryFormatter bf = new BinaryFormatter();
        using (var ms = new MemoryStream())
        {
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }
    }
xamarin.ios
1个回答
0
投票

使用其他转换。

首先,将您的UIImage转换为Stream。要做到这一点,请使用'myUIImage.AsPng()。AsStream()'。然后将'Stream'转换为'Byte Array'。为此,请使用:

public static byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16*1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

然后你可以安全地序列化它。

要从字节数组制作图像,您可以使用此扩展名

private UIImage ImageFromBytes(byte[] bytes, nfloat width, nfloat height)
{
    try {
        NSData data = NSData.FromArray(bytes);
        UIImage image = UIImage.LoadFromData(data);
        CGSize scaleSize = new CGSize(width, height);
        UIGraphics.BeginImageContextWithOptions(scaleSize, false, 0);
        image.Draw(new CGRect(0,0, scaleSize.Width, scaleSize.Height));
        UIImage resizedImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
        return resizedImage;
    } catch (Exception) {
        return null;
    }
}

相信这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.