Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

xaml - Loading multiple images from file path and assigning them to image control inside collection view sometimes does not load properly

There are around 30-40 images that gets downloaded from the server and saved to local and then file path is passed through which image source is generated. Download call is made whenever user scrolls the collectionview. Sometimes images are getting loaded completely and sometime partially.

                    <CollectionView 
                            HorizontalOptions="FillAndExpand"
                            HorizontalScrollBarVisibility="Never"
                            VerticalScrollBarVisibility="Never"
                            SelectionMode="Single"
                            ItemSizingStrategy="MeasureAllItems"
                            ItemsSource="{Binding Items}">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Image}"></Image>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                 </CollectionView>

C#

   await Utility.FileHanler.Download(Id, resourceTypeId, material, ThumbURL, token, (arg) =>
                            {
                                
                                LoadImage(arg);
                            });


  public async Task Download(int id, int resourceType,string fileName,string thumbnailUrl, CancellationToken token , Action<string> CompletionHandler)
    {
        string destinationFolder;
        
        destinationFolder = Path.Combine(FileHandler.Images, id.ToString());
        
        try
        {
            if (!Directory.Exists(destinationFolder))
            {
                Directory.CreateDirectory(destinationFolder);
            }
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            token.ThrowIfCancellationRequested();
            WebRequest wr = WebRequest.Create(thumbnailUrl + App.Token);
            WebResponse ws = await wr.GetResponseAsync();
            Stream s = ws.GetResponseStream();
            byte[] buffer = new byte[10240];
            var filePath = Path.Combine(destinationFolder, fileName);
            var dlStream = new FileStream(filePath, FileMode.Create);
            int readBytes = s.Read(buffer, 0, 10240);
            while (readBytes > 0)
            {
                dlStream.Write(buffer, 0, readBytes);
                readBytes = s.Read(buffer, 0, 10240);
            }
            dlStream.Close();
            dlStream.Dispose();
            s.Close();
            s.Dispose();
            ws.Close();
            CompletionHandler?.Invoke(filePath);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    public void LoadImage(string filePath)
    {
          Device.BeginInvokeOnMainThread(() =>
          {
           Image = ImageSource.FromFile(filePath);
          });
    }
       
      
             

CLipped Image

Image Loaded fully


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...