【问题标题】:App crashes if null image URL is loaded in JSON object如果在 JSON 对象中加载空图像 URL,则应用程序崩溃
【发布时间】:2014-02-24 04:19:42
【问题描述】:

我正在通过 JSON 使用 MySQL 表中的对象填充 tableView。 很多用户在网站表单中引入源数据,有时他们不会在字段中引入图像 URL。

下载的 JSON 对象显示在 tableView 中,其中行被配置为显示文本、详细文本和图像。

如果对象没有图片 URL,应用程序崩溃,我想显示默认图片,以避免崩溃。

这就是我为对象图像加载 JSON 数据的方式:

    NSMutableString *logo = [[NSMutableString alloc]initWithString:@"http://mujercanariasigloxxi.appgestion.eu/logos/"];
    NSString *imageURL = [[categorias objectAtIndex:indexPath.row] objectForKey:@"strImagen"];
    [logo appendString:imageURL];
    NSURL *logoURL = [NSURL URLWithString:logo];
    NSData *logoData = [NSData dataWithContentsOfURL:logoURL];


    cell.imageView.image = [UIImage imageWithData:logoData];

如果网站用户未在 Web 表单中包含图像 URL,那么避免崩溃和显示默认图像的最佳方法应该是什么?

【问题讨论】:

  • 你需要检查 imageURL,如果它是 nil 或空然后加载你的默认图像。
  • 检查图像,如果它为 nil,则从服务器加载默认图像,否则加载您的图像。
  • 你也可以在那里设置占位符图片。
  • @Manthan,占位符图片是什么意思?
  • 您可以将这个github.com/rs/SDWebImage 用于来自服务器的图像。如果图像不是从服务器加载的,您可以显示占位符图像。

标签: ios json uitableview


【解决方案1】:

尝试使用if-else 条件,例如

NSMutableString *logo = [[NSMutableString alloc]initWithString:@"http://mujercanariasigloxxi.appgestion.eu/logos/"];

NSData *logoData;
if([categorias objectAtIndex:indexPath.row] && [[categorias objectAtIndex:indexPath.row] objectForKey:@"strImagen"]){
   NSString *imageURL = [[categorias objectAtIndex:indexPath.row] objectForKey:@"strImagen"];
   [logo appendString:imageURL];
   NSURL *logoURL = [NSURL URLWithString:logo];
  logoData = [NSData dataWithContentsOfURL:logoURL];
}
else {
   //load your default image here
   logoData = //default logo data
}


cell.imageView.image = [UIImage imageWithData:logoData];

【讨论】:

    【解决方案2】:
    //set an activity indicator                 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        NSString *imageURL = [[categorias objectAtIndex:indexPath.row] objectForKey:@"strImagen"];
        if (imageURL.count>0){
            [logo appendString:imageURL];
    
            //declare logodata as nsdata
            logoData=[NSData dataWithContentsOfURL:[NSURL URLWithString:logo]];
        }
    
        dispatch_sync(dispatch_get_main_queue(), ^{
    
            //turn the activity indicator off
            if(logoData!=nil)
                cell.imageView.image = [UIImage imageWithData:logoData];
            else
                //set your default image.
                });
    });
    

    希望这对你有帮助。

    【讨论】:

      【解决方案3】:

      您可以尝试检查 imageurl 是否为nil,或者在某些情况下它会为null

      NSMutableString *logo = [[NSMutableString alloc]initWithString:@"http://mujercanariasigloxxi.appgestion.eu/logos/"];
      NSString *imageURL = [[categorias objectAtIndex:indexPath.row] objectForKey:@"strImagen"];
      
      
      if(imageURL != nil && ![imageURL isEqual:[NSNull null]])
      {
          [logo appendString:imageURL];
          NSURL *logoURL = [NSURL URLWithString:logo];
          NSData *logoData = [NSData dataWithContentsOfURL:logoURL];
          cell.imageView.image = [UIImage imageWithData:logoData];
      }
      else{
          cell.imageView.image = [UIImage imageNamed:@"Default_image"];
      }
      

      【讨论】:

        猜你喜欢
        • 2019-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-10
        相关资源
        最近更新 更多