编码 xamarin 表单项目并使用 pcl 存储。在设备上保存图像或文件时遇到问题我发现的所有示例都显示了如何将字节数组流保存到文件中,但没有一个示例显示如何将图像转换为可用格式以进行保存。
var webImage = new Image();
webImage.Source = new UriImageSource
{
CachingEnabled = false,
Uri = new Uri("http://server.com/image.jpg"),
};
byte[] image = ????(what i need)????(webImage.Source);
// get hold of the file system
IFolder folder = rootFolder ?? FileSystem.Current.LocalStorage;
// create a file, overwriting any existing file
IFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
// populate the file with image data
using (System.IO.Stream stream = await file.OpenAsync(FileAccess.ReadAndWrite))
{
stream.Write(image, 0, image.Length);
}
换句话说,寻找代码以从 url 将图像/文件保存在设备上。
尝试 ffimageloading,因为它包含字节 [] 的转换器,但总是得到错误:“对象引用未设置为对象的实例。” 对于 GetImageAsJpgAsync 和 GetImageAsPngAsync 方法。问题可能是图像未完全加载。但是即使图像完全加载到屏幕上并绑定到缓存图像,也不会调用完成事件和完成命令。
var cachedImage = new CachedImage()
{
Source = "https://something.jpg",
FinishCommand = new Command(async () => await TEST()),
};
cachedImage.Finish += CachedImage_Finish;
var image = await cachedImage.GetImageAsJpgAsync();