简体   繁体   English

xamarin.forms保存文件/图像

[英]xamarin.forms save file/image

coding xamarin form project and also using pcl storage. 编码xamarin表单项目,并使用pcl存储。 have problem with saving image or file on device all examples i found show how to save stream of byte array into file but non of them show how to turn convert image into usable format for saving. 在设备上保存图像或文件时遇到问题我发现的所有示例都显示了如何将字节数组流保存到文件中,但没有一个示例显示了如何将图像转换为可用格式进行保存。

        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);
        }

In other words looking for code to save image/file on device from url. 换句话说,要寻找代码以从URL将图像/文件保存在设备上。

Tried ffimageloading since it contain converter to byte[], but always get error:"Object reference not set to an instance of an object." 尝试ffimageloading,因为它包含转换为byte []的转换器,但始终会收到错误:“对象引用未设置为对象的实例。” for GetImageAsJpgAsync and GetImageAsPngAsync method. 用于GetImageAsJpgAsync和GetImageAsPngAsync方法。 problem might be that image isn't fully loaded. 问题可能是图像未完全加载。 but finish event and finish command never get called even trough image is fully loaded on screen and bound to cachedImage. 但是即使槽图像已完全加载到屏幕上并绑定到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();

With FFImageLoading (I noticed you use it): 使用FFImageLoading(我注意到您使用了它):

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

If you want to get original file location, use this: 如果要获取原始文件位置,请使用以下命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM