Loading images from a URL into a UITableViewCell's UIImageView

前端 未结 4 2122
面向向阳花
面向向阳花 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:07

    You can load data this way:

    NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]];
    

    And you can instantiate the array of URLs this way too:

    NSArray *newsImageURL = [imagesURL componentsSeparatedByString:@","];
    

    However, if someone scrolls around on the table a great deal, you may end up loading the images many times over as the cells are recycled.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-14 14:08

    You Can Easily load all the images with the Help of Following code ,Details Array is a Main Array

    Details Array :-  {
            "item_code" = 709;
            "item_desc" = Qweqweqwe;
            "item_name" = AQA;
            "item_photo" = "http://toshaya.com/webapp/snap&sell/api/img_items/709.png";
            "item_price" = "0.00";
            "item_till" = "20-25";
            "item_type" = Orange;
            latitude = "";
            longitude = "";
        }
    

    With the Help of Following Code Retrieve The Photo-URL into String

     NSString * result = [[DetailArray objectAtIndex:indexPath.row]objectForKey:@"item_photo"]; //componentsJoinedByString:@""];
    
     NSLog(@"%@",result);
    
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:result]];
    cell.icon.image = [UIImage imageWithData:imageData];
    
    0 讨论(0)
  • 2020-12-14 14:15

    Maybe you can have a try https://github.com/SpringOx/ALImageView.git.It is much simpler than SDWebImage.You only need two source files(ALImageView.h/ALImageView.m).You can reuse the image view to reload different urls in a tableview cell.

    1. Support local and memory cache;
    2. Support place holders;
    3. Support tap touch(target-action);
    4. Support corner for the image view;
    0 讨论(0)
提交回复
热议问题