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](https://i.stack.imgur.com/owg8D.png)
![Image Loaded fully](https://i.stack.imgur.com/TkgbA.png)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…