UWP开发中两种网络图片缓存方法

来源:http://www.goldns.net 作者:手机APP 人气:114 发布时间:2019-10-02
摘要:1.WriteableBitmap public async Task SaveImageAsync(WriteableBitmap image, string filename) { try { if (image == null) { return; } Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId; if (filename.EndsWith("jpg")) BitmapEncoderGuid = Bitmap

1.WriteableBitmap

 public async Task SaveImageAsync(WriteableBitmap image, string filename)
        {
            try
            {
                if (image == null)
                {
                    return;
                }
                Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
                if (filename.EndsWith("jpg"))
                    BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
                else if (filename.EndsWith("png"))
                    BitmapEncoderGuid = BitmapEncoder.PngEncoderId;
                else if (filename.EndsWith("bmp"))
                    BitmapEncoderGuid = BitmapEncoder.BmpEncoderId;
                else if (filename.EndsWith("tiff"))
                    BitmapEncoderGuid = BitmapEncoder.TiffEncoderId;
                else if (filename.EndsWith("gif"))
                    BitmapEncoderGuid = BitmapEncoder.GifEncoderId;
                var folder = await _local_folder.CreateFolderAsync("images_cache", CreationCollisionOption.OpenIfExists);
                var file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
                    Stream pixelStream = image.PixelBuffer.AsStream();
                    byte[] pixels = new byte[pixelStream.Length];
                    await pixelStream.ReadAsync(pixels, 0, pixels.Length);
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                              (uint)image.PixelWidth,
                              (uint)image.PixelHeight,
                              96.0,
                              96.0,
                              pixels);
                    await encoder.FlushAsync();
                }
            }
            catch
            {

            }
        }

public async Task WriteToFileAsync(StorageFolder folder,SoftwareBitmap sb,string fileName)
        {

            if (sb != null)
            {
                // save image file to cache
                StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                    encoder.SetSoftwareBitmap(sb);
                    await encoder.FlushAsync();
                }
            }
        }

  常常情状下,我们的顺序必要从服务器读取图片,但假设供给不唯有二次读取某一张图纸的话,就须求做地点缓存了,那样既为客商省一点流量,又能突显你的APP异常的快。

public async Task<SoftwareBitmap> GetSoftwareBitmapAsync(string url)
        {
            try
            {
                IInputStream inputStream = await GetSteramAsync(url);
                IRandomAccessStream memStream = new InMemoryRandomAccessStream();
                await RandomAccessStream.CopyAsync(inputStream, memStream);
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memStream);
                SoftwareBitmap sb = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                return sb;
            }
            catch 
            {
                return null;
            }
        }
          SoftwareBitmapSource sbs = new SoftwareBitmapSource();
                sbs.SetBitmapAsync(sb);

这边有二种办法:

什么,是否很简短?

2.SoftwareBitmap

接下去便是保存了:WriteableBitmap:

 async static public Task<IInputStream> GetStreamAsync(string url)
        {

            httpClient = new HttpClient();
            var response = await httpClient.GetInputStreamAsync(new Uri(url));
            return response;
        }

 async static public Task<IBuffer> GetBufferAsync(string url)
        {

            httpClient = new HttpClient();

            var ResultStr = await httpClient.GetBufferAsync(new Uri(url));
            return ResultStr;
        }

  当大家下载完毕后,首先要做的很有相当大希望是先出示出来,然后再保存,所以先把数量写入到图片对象中:

那三种都足以充作展现图像的数据源,在那之中WriteableBitmap能够从来给Image.Source , SoftwareBitmap那亟需转为SoftwareBitmap:

手机APP,那四个静态方法分获url地址的buffer数据和输入流。有了buffer也许stream之后就足以扩充下一步-保存。

 protected async Task<WriteableBitmap> GetWriteableBitmapAsync(string url)
        {
            try
            {
                IBuffer buffer = await GetBufferAsync(url);
                if (buffer != null)
                {
                    BitmapImage bi = new BitmapImage();
                    WriteableBitmap wb = null; Stream stream2Write;
                    using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
                    {

                        stream2Write = stream.AsStreamForWrite();

                        await stream2Write.WriteAsync(buffer.ToArray(), 0, (int)buffer.Length);

                        await stream2Write.FlushAsync();
                        stream.Seek(0);

                        await bi.SetSourceAsync(stream);

                        wb = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight);
                        stream.Seek(0);
                        await wb.SetSourceAsync(stream);

                        return wb;
                    }
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }
        }

  假设你早就领悟了某一张图纸的地方,那么首先件事便是要把这张图片下载下来;当然要是是一回性读取的话,能够一贯把图纸地址给Image控件大概给Bitmapimage对象(实际上那二者是从未去别的),但这无法存到本地,只充当展现用;可是大家要做的是保留到地面,那样自然是极其的。现在大家就要动用HTTP的事物了,请看上面包车型地铁代码:

本文由澳门新葡亰娱乐场发布于手机APP,转载请注明出处:UWP开发中两种网络图片缓存方法

关键词:

上一篇:没有了

下一篇:没有了

频道精选

最火资讯