【问题标题】:How to use SDWebImage如何使用 SDWebImage
【发布时间】:2023-04-06 04:54:01
【问题描述】:

我有 UITableViewCell 并在此加载图像,所以我想加载图像并显示激活指示器。这次图像加载时间非常长,所有图像在图像显示在表格视图后加载。很多时候图像无法加载并且用户 UIInterface 挂起。并且不要在应用程序中处理任何进程。我很累,我使用了SDWebImage 库。请帮助如何可能,谢谢

#import "UIImageView+WebCache.h"

@interface pictureViewController ()
{
    NSArray*picarray;
    NSArray*titlearray;
    NSArray*idarray;
}

@end

@implementation pictureViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[[self navigationController] navigationBar] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"background2.png"] forBarMetrics:UIBarMetricsDefault];

    _tableview.separatorColor = [UIColor yellowColor];

    NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_gallery.php"]];
    response =[[NSMutableData alloc]init];
    [NSURLConnection connectionWithRequest:req delegate:self];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [response appendData:data];
    NSLog(@"error receving data %@",response);
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     NSError *error;

     //NSLog(@"Error in receiving data %@",error);
     NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves  error:&error];
     // NSLog(@"response data %@",json);

      NSArray *results = [json objectForKey:@"status"];
      picarray = [results valueForKey:@"img_url"];
      titlearray = [results valueForKey:@"title"];
      // NSLog(@"my title is%@",titlearray);
      idarray=[results valueForKey:@"id"];
      //NSLog(@"my galary id is%@",idarray);
      [self.tableview reloadData];
}


// Do any additional setup after loading the view, typically from a nib.
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [picarray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // NSLog(@"tableview cell");
    picture_viewcontrollerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htr"];
    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

     //NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [picarray objectAtIndex:indexPath.row]]];
    // UIImage* image = [[UIImage alloc] initWithData:imageData];
    //cell.picture.image =image;

    cell.titlelbl.text=[NSString stringWithFormat:@"%@",[titlearray objectAtIndex:indexPath.row]];

    NSURL*url= [[picarray objectAtIndex:indexPath.row]valueForKey:@"img_url"];
    [cell.picture setContentMode:UIViewContentModeScaleAspectFill];
    [cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]];


    return  cell;
}

@end

【问题讨论】:

    标签: ios objective-c iphone image uitableview


    【解决方案1】:

    您确定您的代码甚至可以编译吗?在您的 cellForRowAtIndexPath 中,当您注释掉您的 imageData 声明时,我看到了 [cell.picture sd_setImageWithURL:imageData placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]];

    即使你没有评论,这一行也应该是

    [cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]];
    

    【讨论】:

    • 抱歉编辑我的代码。请再看看我的问题
    【解决方案2】:

    你必须使用

    NSURL*url= [[picarray objectAtIndex:indexPath.row]valueForKey:@"img_url"];
    [cell.picture setContentMode:UIViewContentModeScaleAspectFill];
    [cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]];
    

    而不是

    NSURL*url= [[picarray objectAtIndex:indexPath.row]valueForKey:@"img_url"];
    [cell.picture setContentMode:UIViewContentModeScaleAspectFill];
    [cell.picture sd_setImageWithURL:imageData placeholderImage:[UIImage imageNamed:@"Placeholder.jpg"]];
    

    你必须直接传递 url 而不是 imageData

    【讨论】:

      【解决方案3】:

      试试这个代码:

      1. 在表格视图单元格中添加UIActivityIndicatorView
      2. 在您的cellForRowAtIndexpath 中更改了以下代码。

        cell.activityIndicator.center = imageView.center;
        cell.activityIndicator.hidesWhenStopped = YES;
        NSURL *url= [[picarray objectAtIndex:indexPath.row]valueForKey:@"img_url"];
        [cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                              completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                 [cell.activityIndicator stopAnimating];
                              }];
        [cell.picture addSubview:activityIndicator];
        [cell.activityIndicator startAnimating];
        

      【讨论】:

      • 在 UITableViewCell 类“activityIndi​​cator”中声明?
      • @A.Kumawat 是的,将它添加到 UITableViewCell 中。
      • 最后一行 [activityIndi​​cator startAnimating];没有声明
      • 我还有一个问题,假设我在视图控制器上没有 tableview 只有一个图像视图。那么它如何异步加载图像。
      • 为此,您可以传递“cell.picture”的“imageview”。
      【解决方案4】:

      你应该试试这个(显示activityIndi​​cator & Load Image using SDWebImage)-

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          // NSLog(@"tableview cell");
          picture_viewcontrollerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htr"];
          if (cell==nil)
          {
              NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
              cell = [nib objectAtIndex:0];
          }
      
          cell.titlelbl.text=[NSString stringWithFormat:@"%@",[titlearray objectAtIndex:indexPath.row]];
      
          NSURL*url= [NSURL URLWithString:[NSString stringWithFormat:@"%@",[picarray objectAtIndex:indexPath.row]]];
      
          [cell.picture setContentMode:UIViewContentModeScaleAspectFill];
      
          [cell.picture setShowActivityIndicatorView:YES];
      
          [cell.picture setIndicatorStyle:UIActivityIndicatorViewStyleGray];
      
          [cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolder.png"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
      
          return  cell;
      
      }
      

      【讨论】:

      • '[<__nscfstring> valueForUndefinedKey:]:这个类对于键 img_url 不符合键值编码。'
      • 更改这一行 - NSURL*url= [NSURL URLWithString: [[picarray objectAtIndex:indexPath.row]valueForKey:@"img_url"]];使用您的上一行并检查此错误是否存在..检查我的代码中的更新行..
      • '[<__nscfstring> valueForUndefinedKey:]:此类不符合键 img_url 的键值编码。同样的错误显示如下
      • @A.Kumawat 您的 picarray 包含它不包含字典对象的 ImageURL。所以可以使用indexPath.row直接获取imageUrl。无需使用 valueForKey..
      • 试试我更新的答案。希望这能解决你的问题。
      猜你喜欢
      • 2014-02-16
      • 2013-08-03
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 2019-01-20
      • 2016-09-22
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多