Images in UITableView keep re-loading and wrong images flash while scrolling

前端 未结 6 1403
醉酒成梦
醉酒成梦 2021-01-16 19:40

i have created a UITableView which populates each cell from URL requests. I have used \'dispatch_queue\' to prevent the UItableView from freezing. For some reason when i s

6条回答
  •  半阙折子戏
    2021-01-16 20:39

    You should consider using this library SDWebImage, available here https://github.com/rs/SDWebImage for that kind of problems. It handles asynchronous download and cache for remote images very easily.

    The simpliest installation is done by using CocoaPods

    CocoaPods (http://cocoapods.org) is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries in your projects. See the Get Started section for more details.

    Use in your Podfile

    platform :ios, '6.1'
    pod 'SDWebImage', '~>3.6'
    

    After installing the dependency for SDWebImage, just simply use the following lines in your view controller :

    #import 
    
    ...
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *MyIdentifier = @"MyIdentifier";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier] autorelease];
        }
    
        // Here we use the new provided setImageWithURL: method to load the web image
        [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
        cell.textLabel.text = @"My Text";
        return cell;
    }
    

提交回复
热议问题