SDWebImage does not load remote images until scroll

前端 未结 4 2064
清歌不尽
清歌不尽 2020-12-13 07:39

I am using SDWebImage library to load remote images into a table view which uses a custom cell class i have created. I simply use

[cell.imageView setImageWit         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 08:24

    I just had to solve this exact problem and didn't want the overhead of the prefetcher. There must be some extra under-the-hood stuff happening with the built-in imageView property that prevents the loading, because a new UIImageView works just fine.

    My solution is pretty clean if you don't mind (or are already) using a subclass of UITableViewCell:

    1. Subclass UITableViewCell.
    2. In your subclass, hide self.imageView.
    3. Create your own UIImageView subview and set this view's image.

    Here's a modified version of my own code (undocumented here is setting the frame to match the size & position of the iOS Photo app's album covers):

    YourTableCell.h

    @interface YourTableCell : UITableViewCell
        @property (nonatomic, strong) UIImageView *coverPhoto;
    @end
    

    YourTableCell.m

    @implementation YourTableCell
    
    @synthesize coverPhoto;
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            self.imageView.image = nil;
            self.coverPhoto = [[UIImageView alloc] init];
    
            // Any customization, such as initial image, frame bounds, etc. goes here.        
    
            [self.contentView addSubview:self.coverPhoto];
        }
        return self;
    }
    //...
    @end
    

    YourTableViewController.m

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        YourTableCell *cell = (YourTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        //...
        [cell.coverPhoto setImageWithURL:coverUrl placeholderImage:nil options:SDWebImageCacheMemoryOnly];
        //...
    }
    

提交回复
热议问题