2016-06-10 14 views
0

我试图从MWFeedParser的RSS Feed中获取图像。我收到Feed项目,但是当我尝试访问图片时,content值中缺少某些内容。它必须在那里,但事实并非如此。MWFeedParser RSS阅读器没有得到图像

所以我改变了链接的内容值。 Link是这样的:

http://www.uefa.com/uefaeuro/news/newsid=2372826.html?rss=2372826+Italy\'s+\'BBC\'+spell+out+programme+for+solidity

然后现在match值越来越为零。我不知道有什么问题。

这是我的RSS提要URL就是我使用到:

let url = NSURL(string: "feed://www.uefa.com/rssfeed/uefaeuro/rss.xml") 

这是我的代码,从RSS源获取图片:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("homeFeed", forIndexPath: indexPath) as! HomeFeedTableViewCell 

     cell.homeFeedImageView.image = UIImage(named: "placeholder") 

     let item = feedItem[indexPath.row] as MWFeedItem? 
     cell.homeFeedTextView.text = item?.title 

     if item?.link != nil{ 
      let htmlContent = item!.link as NSString 
      var imageSource = "" 

      let rangeOfString = NSMakeRange(0, htmlContent.length) 
      do{ 
      let regex = try NSRegularExpression(pattern: "<img.*?src=\"([^\"]*)\"", options: []) 

       if htmlContent.length > 0 { 
        let match = regex.firstMatchInString(htmlContent as String, options: [], range: rangeOfString) 
        if match != []{ 
         let imageUrl = htmlContent.substringWithRange((match!.rangeAtIndex(2))) as NSString 
         print(imageUrl) 

         if NSString(string: imageUrl.lowercaseString).rangeOfString("feedburner").location == NSNotFound{ 
          imageSource = imageUrl as String 
         } 
        } 

       } 

       if imageSource != ""{ 
        cell.homeFeedImageView.setImageWithURL(NSURL(string: imageSource)!, placeholderImage: UIImage(named: "placeholder")) 
       }else{ 
        cell.homeFeedImageView.image = UIImage(named: "placeholder") 
       } 
      } 
      catch let error as NSError{ 
       print(error.localizedDescription) 
      } 
     } 


     return cell 
    } 

回答

1

尝试设置与图像代替的URLRequest,如下图所示:

if imageSource != ""{ 
     let request = URLRequest(url: Foundation.URL(string: imageSource)!) 
     cell.imageView?.setImageWith(request, placeholderImage: nil, success: { [weak cell] request, response, image in 
     if cell != nil { 
      cell!.imageView!.image = image 
     } 

     if self.tableView.visibleCells._bridgeToObjectiveC().contains(cell!) { 
      self.tableView.reloadRows(at: [indexPath], with: .none) 
     } 
     }, failure:nil) 
} 

working screenshot of your RSS

截图中的图片很小,但是这是由于我的imageview配置。我猜你的imageview被配置为适合更大的图像。

祝你好运!

***注意:上面的代码是用于Swift 3.