如何优化大量图片的加载?

我从 REST 服务上传大量图像,并通过定期任务使用它们创建各种 gif 图像。我想知道如何优化我的代码,改进它并使其更快。


 public class WebcamListViewModel : BaseViewModel

{

    public ICommand InitializeWebcamsCommand { set; get; }

    public ICommand OpenVideoWebcamCommand { set; get; }


private List<Webcam> _ListOfWebcam { get; set; }

public List<Webcam> ListOfWebcam

{

    get { return _ListOfWebcam; }

    set

    {

        _ListOfWebcam = value;

        OnPropertyChanged();

    }

}


private IFolder folder;

private int _Counter { get; set; }

public int Counter

{

    get { return _Counter; }

    set

    {

        _Counter = value;

        OnPropertyChanged();

    }

}


private Task SetFrameOnViewTask;


private Task DownloadFramesTask;


CancellationTokenSource tokenSourceSetFrame = new CancellationTokenSource();


CancellationTokenSource tokenSourceDownloadFrames = new CancellationTokenSource();


CancellationToken cancellationTokenSetFrame;


CancellationToken cancellationTokenDownloadFrames;


public WebcamListViewModel(INavigationService navigationService, IApiAutostradeManagerFactory apiAutostradeManagerFactory) : base(navigationService,apiAutostradeManagerFactory)

{


    OpenVideoWebcamCommand = new Command<Webcam>(async (webcam) => {

        await navigationService.NavigateAsync(Locator.WebcamVideoPopUpPage);

        Messenger.Default.Send(new InfoWebcamVideoMessage(webcam.c_mpr, webcam.c_uuid, webcam.t_str_vid));

    });


    InitializeWebcamsCommand = new Command(async () => await RunSafe(InitializeWebcams()));

    InitializeWebcamsCommand.Execute(null);


    cancellationTokenDownloadFrames = tokenSourceDownloadFrames.Token;


    DownloadFramesTask = new Task(async () => {

        cancellationTokenDownloadFrames.ThrowIfCancellationRequested();




在我的 viewModel 中,我有两个任务:DownloadFramesTask 和 SetFrameOnViewTask,每 500 毫秒增加一个计数器,用于显示轮流中的四个帧之一。


qq_笑_17
浏览 68回答 1
1回答

波斯汪

而不是这个:foreach (var web in ListOfWebcam){&nbsp; &nbsp; web.image1 = await GetWebcamFrame(web.frame1);&nbsp; &nbsp; web.image2 = await GetWebcamFrame(web.frame2);&nbsp; &nbsp; web.image3 = await GetWebcamFrame(web.frame3);&nbsp; &nbsp; web.image4 = await GetWebcamFrame(web.frame4);}您可以一次执行所有异步任务:var tasks = new List<Task>();tasks.Add(GetWebcamFrame(web.frame1));// add more tasks hereawait Task.WhenAll(tasks);Task.Delay()您还可以删除对;的使用。目前还不清楚为什么有必要这样做。
打开App,查看更多内容
随时随地看视频慕课网APP