【问题标题】:IOS Swift Json File with a Image Url带有图像 URL 的 IOS Swift Json 文件
【发布时间】:2016-04-14 13:22:35
【问题描述】:

我有一个 Json 文件,其中有一个类似这样的项目:“image”:“https://xxxxxxxxxxx.jpg

所以我正在尝试这个:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell : TableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell

    let strName : NSString=arrDict[indexPath.row] .valueForKey("name") as! NSString
    let strSubtitle : NSString=arrDict[indexPath.row] .valueForKey("subtitle") as! NSString
    let strLocation : NSString=arrDict[indexPath.row] .valueForKey("location") as! NSString
    let strStart : NSString=arrDict[indexPath.row] .valueForKey("start") as! NSString
  //  let strImage : NSString=arrDict[indexPath.row] .valueForKey("image") as! NSString

    // let strImage : NSData=arrDict[indexPath.row] .valueForKey("image") as! NSData

    let strImage : UIImage=arrDict[indexPath.row] .valueForKey("image") as! UIImage

    cell.lbName.text=strName as String
    cell.lbSubtitle.text=strSubtitle as String
    cell.lbStart.text=strStart as String
    cell.lbLocation.text=strLocation as String
    cell.lbImage.image=strImage as UIImage

    return cell as TableViewCell
}

在运行此消息后显示:

无法将“__NSCFString”(0x1112ca090) 类型的值转换为“UIImage”(0x11222b800)。

谢谢。

【问题讨论】:

  • arrDict[indexPath.row] .valueForKey("image") 似乎是一个 NSString 对象。你需要下载它:stackoverflow.com/questions/24231680/…
  • 停止使用valueForKey: 来简单地获取字典键的值。专用方法是objectForKey:或键下标。

标签: ios json swift uiimage


【解决方案1】:

问题出在这一行let strImage : UIImage=arrDict[indexPath.row] .valueForKey("image") ..

我确定您的 arrDict[indexPath.row].valueForKey("image") 返回 String ... 所以您不能直接将其转换为 UIImage ..

if let strImage = arrDict[indexPath.row]["image"] as? String{
    // ... your strImage is String  ... 
     if let data = NSData(contentsOfURL: NSURL(string:strImage )) {
          cell.lbImage.image = UIImage(data: data)
     }        
}

您的问题的第一行建议 "image":"https://xxxxxxxxxxx.jpg" .. 所以通过将您的 Sting 转换为 NSURL 并下载图像来从 url 获取图像

您可以使用 SDWebImage 显示来自 URL https://github.com/rs/SDWebImage 的图像,或者您可以使用带有 contentsOfURL 的 NSData 类

更多信息请查看this link

【讨论】:

    【解决方案2】:

    问题是

    let strImage : UIImage=arrDict[indexPath.row] .valueForKey("image") as! UIImage
    

    您正在尝试使用 as! UIImage 强制将 String 强制转换为 UIImage,您应该只在 100% 确定时才使用 !

    代替
    if let image = arrDict[indexPath.row] .valueForKey("image") as? UIImage {
        //Now you have a UIImage
    }
    

    但在你的情况下,它不是 UIImage 它是 String 所以

     if let imageString = arrDict[indexPath.row] .valueForKey("image") as? String {
            //Now you have a String
            // Now make a UIImage from URL and insert it.
        }
    

    【讨论】:

    • 谢谢。如果你能帮助我,因为我很难从 URL 制作 UIImage 并插入......代码是在 JsonFile 或 Json Url 中打开。
    • 这是示例 { "objects":[ { "image":"xxxxxxxxxxxxxxxx.jpg" }, { "image":"xxxxxxxxxxxxxxxx.jpg" } ] }
    • 你好。请你看看我在个人资料中的最后一个问题。我还必须在 JSON 的 tableVC 单元格中设置徽标。我可以像你建议的那样解决我的问题吗?我试过这个“if let imageString = data[indexPath.row] .valueForKey("logo") as?String” 在我的例子中数据是 [String:Id] 数组。
    • 看起来现在一切都已修复@Abrcd18:D
    【解决方案3】:

    通过小伙伴们的代码和指令,问题解决了。谢谢。

    if let strImage = arrDict[indexPath.row]["image"] as? String{
    // ... your strImage is String  ... 
     if let data = NSData(contentsOfURL: NSURL(string:strImage )) {
          cell.lbImage.image = UIImage(data: data)
     }        
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多