【问题标题】:xamarin.forms save file/imagexamarin.forms 保存文件/图像
【发布时间】:2018-03-05 05:06:23
【问题描述】:

编码 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 方法。问题可能是图像未完全加载。但是即使图像完全加载到屏幕上并绑定到缓存图像,也不会调用完成事件和完成命令。

var cachedImage = new CachedImage()
{
     Source = "https://something.jpg",
     FinishCommand = new Command(async () => await TEST()),
};
cachedImage.Finish += CachedImage_Finish;


var image = await cachedImage.GetImageAsJpgAsync();

【问题讨论】:

    标签: c# xamarin.forms ffimageloading


    【解决方案1】:

    使用 FFImageLoading(我注意到你使用它):

    await ImageService.Instance.LoadUrl("https://something.jpg").AsJpegStream();

    如果要获取原始文件位置,请使用:

    await ImageService.Instance.LoadUrl("https://something.jpg").Success((imageInfo) => { //TODO imageInfo.FilePath } ).DownloadOnly();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 2013-05-31
      • 1970-01-01
      相关资源
      最近更新 更多