首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MWFeedParser RSS阅读器无法获取图像

MWFeedParser RSS阅读器无法获取图像
EN

Stack Overflow用户
提问于 2016-06-10 08:08:55
回答 1查看 275关注 0票数 0

我正在尝试使用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,我用来:

代码语言:javascript
复制
let url = NSURL(string: "feed://www.uefa.com/rssfeed/uefaeuro/rss.xml")

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

代码语言:javascript
复制
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
    }
EN

回答 1

Stack Overflow用户

发布于 2016-12-06 02:35:43

尝试使用URLRequest设置镜像,如下图所示:

代码语言:javascript
复制
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的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37738072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档