我正在尝试使用MWFeedParser从RSS Feed获取图像。我正在获取提要项,但是当我尝试访问图像时,content值中缺少了一些东西。它必须在那里,但它不在那里。
所以我用link修改了内容的值。链接是这样的:
http://www.uefa.com/uefaeuro/news/newsid=2372826.html?rss=2372826+Italy\'s+\'BBC\'+spell+out+programme+for+solidity
然后,现在match值变得为零。我不知道问题出在哪里。
这是我的RSS Feed url,我用来:
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
}发布于 2016-12-06 02:35:43
尝试使用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配置。我猜你的图像视图被配置为适合更大的图像。
祝好运!
*注意:上面的代码是针对Swift 3的。
https://stackoverflow.com/questions/37738072
复制相似问题