Loading images from a URL into a UITableViewCell's UIImageView

前端 未结 4 2121
面向向阳花
面向向阳花 2020-12-14 13:26

I have the following code: (Note: newsImageURL is an NSArray)

NSString *imagesURL = @\"http://aud.edu/images/newsimage01.png,http:/         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 14:08

    You should use an existing framework which supports caching, default place holders and lazy loading of images.

    https://github.com/rs/SDWebImage is a good and simple framework

    #import "UIImageView+WebCache.h"
    
    ...
    
    - (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://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png"]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
        cell.textLabel.text = @"My Text";
        return cell;
    }
    

提交回复
热议问题