我试图通过MWFeedParser从RSS Feed获取图像。我收到了Feed项,但是当我尝试访问图片时,content
值中缺少某些内容。它必须存在,但事实并非如此。
所以我用链接更改了内容值。链接是这样的:
然后现在match
值变为零。我不知道问题是什么。
这是我使用的RSS Feed网址:
let url = NSURL(string: "feed://www.uefa.com/rssfeed/uefaeuro/rss.xml")
这是我从RSS Feed获取图片的代码:
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
}
答案 0 :(得分: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。