我试图首先将Image转换为字节并使用Xamarin iOS中的GZipStream将其压缩。这是代码
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var zipFileName = Path.Combine(documentsPath, DateTime.Now.ToString("MMddyyyyHHmmss")+".zip");
using (FileStream fs = new FileStream(zipFileName, FileMode.Create))
using (GZipStream gz = new GZipStream(fs, CompressionMode.Compress, false))
{
gz.Write(dataBytes, 0, dataBytes.Length);``
}
它压缩文件。当我解压缩它以验证图像时,它返回NULL。这是代码
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var zipFileN = Path.Combine(documentsPath, zipFile);
byte[] file = File.ReadAllBytes(zipFileN);
byte[] decompressed = Decompress(file);
UIImage newImg = GetImagefromByteArray(decompressed);
public static UIImage GetImagefromByteArray(byte[] imageBuffer)
{
try
{
NSData data = NSData.FromArray(imageBuffer);
UIImage image = UIImage.LoadFromData(data);
return image; // image GETTING NULL
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
newImg变为NULL。原始图像采用PNG格式。当我比较原始图像的字节和解压缩图像时,它们是相同的。