loading image in UITableViewCell asynchronously

痞子三分冷 提交于 2019-12-21 03:01:27

问题


What is the super very easy way to load the image in the UITableViewCell asynchronously say given a imageURL without having to subclassing the UITableViewCell, i.e: standard UITableViewCell


回答1:


The easiest way I know is to use SDWebImage library. Here is a link that explains how to utilize the SDWebImage library to load avatars asynchronously.

SDWebImage is an extension to the ImageView. Here is the usage:

// load the avatar using SDWebImage
    [cell.imageView setImageWithURL:[NSURL URLWithString:tweet.profileImageUrl]
                   placeholderImage:[UIImage imageNamed:@"grad_001.png"]];

And here is the referenced article:

Implementing Twitter Search




回答2:


In your .m, include the objective c runtime:

#import <objc/runtime.h>

At the top of your @implementation section, define a static constant for use below:

static char * const myIndexPathAssociationKey = "";

In your cellForRowAtIndexPath, add the following code:

// Store a reference to the current cell that will enable the image to be associated with the correct  
// cell, when the image subsequently loaded asynchronously. Without this, the image may be mis-applied
// to a cell that has been dequeued and reused for other content, during rapid scrolling. 
objc_setAssociatedObject(cell,
                         myIndexPathAssociationKey,
                         indexPath,
                         OBJC_ASSOCIATION_RETAIN);

// Load the image on a high priority background queue using Grand Central Dispatch.
// Can change priority by replacing HIGH with DEFAULT or LOW if desired.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{
    UIImage *image = ... // Obtain your image here.

    // Code to actually update the cell once the image is obtained must be run on the main queue.
    dispatch_async(dispatch_get_main_queue(), ^{
        NSIndexPath *cellIndexPath = (NSIndexPath *)objc_getAssociatedObject(cell, myIndexPathAssociationKey);
        if ([indexPath isEqual:cellIndexPath]) {
        // Only set cell image if the cell currently being displayed is the one that actually required this image.
        // Prevents reused cells from receiving images back from rendering that were requested for that cell in a previous life.
            [cell setImage:image];
        }
    });
}];

Finally, for supporting best performance when rapidly scrolling on older devices you may want to load the most recently requested images first... for that, see this thread for asynchronously loading cell images using a last-in first-out stack and GCD.




回答3:


You can use a thread. First place the button on a dictionary. Then use the thread. Finally in the method setImage: you can place the image.

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

        [dictionary setObject:url forKey:@"url"];
        [dictionary setObject:image forKey:@"image"];
        [NSThread detachNewThreadSelector:@selector(setImage:) 
                                 toTarget:self 
                               withObject:dictionary];


来源:https://stackoverflow.com/questions/8087010/loading-image-in-uitableviewcell-asynchronously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!