What is the proper method/accepted standard for loading data asynchronously into table view cells?

前端 未结 4 2095
梦毁少年i
梦毁少年i 2021-02-06 03:04

Many apps such as Tweetbot, Twitterrific, Alien Blue, etc. that display images from an API seem to load them in an asynchronous manner. It seems the initial viewable images are

4条回答
  •  伪装坚强ぢ
    2021-02-06 03:18

    I've been using dispatch_async to load data asynchronously into my table view cells:

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        NSData *imageData = [NSData dataWithContentsOfURL:url];
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage *image = [UIImage imageWithData:imageData];
            [imageView setImage:image];
        });
    });
    

    The images I used in this example were only a couple kilobytes large (each) and they loaded very well into the table view.

提交回复
热议问题