![](/img/trans.png)
[英]Xamarin.Forms: How to download an Image, save it locally and display it on screen?
[英]xamarin.forms save file/image
编码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,因为它包含转换为byte []的转换器,但始终会收到错误:“对象引用未设置为对象的实例。” 用于GetImageAsJpgAsync和GetImageAsPngAsync方法。 问题可能是图像未完全加载。 但是即使槽图像已完全加载到屏幕上并绑定到cachedImage,也不会调用finish事件和finish命令。
var cachedImage = new CachedImage()
{
Source = "https://something.jpg",
FinishCommand = new Command(async () => await TEST()),
};
cachedImage.Finish += CachedImage_Finish;
var image = await cachedImage.GetImageAsJpgAsync();
使用FFImageLoading(我注意到您使用了它):
await ImageService.Instance.LoadUrl("https://something.jpg").AsJpegStream();
如果要获取原始文件位置,请使用以下命令:
await ImageService.Instance.LoadUrl("https://something.jpg").Success((imageInfo) => { //TODO imageInfo.FilePath } ).DownloadOnly();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.