如何从xml feed上传ios rss feed表格单元格的缩略图图像

时间:2015-08-19 13:57:56

标签: ios xml xcode rss tableviewcell

几乎完成了我需要为工作创建的rss应用程序。我仍然是xcode的新手,我尝试过许多不同的开源代码。我终于找到了一个更适合我的需求的东西,但我最后需要的是让缩略图像适用于表视图单元格。单元格中的文章标题和描述加载正常,但是当我尝试重新编写图像的代码时,它会显示为空白。

该项目旨在与谷歌新闻rss合作:https://news.google.com/?output=rss。我使用的rs是:http://www3.westchestergov.com/home/all-press-releases?format=feed&type=rss

现在,我认为处理表格视图单元格图像的部分代码是:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    let newsItem = RSSdataList[indexPath.row]
    cell.imageView?.image != nil
    cell.textLabel?.text = newsItem.title
    cell.detailTextLabel?.text = newsItem.summary

    // Use cached image or download new image for the cell
    if let imgURLString = newsItem.imgURL, let cachedImage = imagesCache[imgURLString] {
        cell.imageView?.image = cachedImage

    } else if let imgURLString = newsItem.imgURL {

        // Need to set a blank image to the imageView to show the downloaded image
        let blank = UIImage(named: "Blank.jpg")
        var tempImage = CGImageCreateWithImageInRect(blank?.CGImage!, CGRect(x: 0, y: 0, width: 80, height: 80))
        cell.imageView?.image = UIImage(CGImage: tempImage)

        dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) {
            var downloadedImg = UIImage()

            if let imgURL = NSURL(string: imgURLString), let imgData = NSData(contentsOfURL: imgURL), let img = UIImage(data: imgData) {
                downloadedImg = img
            } else {
                println("Error while downloading img")
            }

            dispatch_async(dispatch_get_main_queue()) {
                if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                    cellToUpdate.imageView?.image = downloadedImg
                }
            }
        }
    }

    return cell
} 

对于谷歌rss这工作正常,当我搜索他们的代码时,我发现他们的图像存储为:

img src="//t3.gstatic.com/images?q=tbn:ANd9GcSHnlu8OJiZBnM5ggUNjTe5ZwBwYWEN_xVjwglmYgUDZOdJSAc5s62-gAgTXTTu4sI8bVRc6W1k" alt="" 

对于我使用的rss图像存储为:

<img style="border: 1px solid #cccccc; margin-bottom: 2px; margin-left: 10px; float: right;" src="http://www3.westchestergov.com/images/stories/newsPrimary2015/ferc-logo.jpg" alt="ferc-logo" width="333" height="222" 

任何人都可以了解我如何能够更改代码以便它可以使用我正在使用的RSS源吗?这是我的最后一块拼图,如果我可以让这个工作,我终于可以把这个应用程序放在我身后。

另外,如果它有帮助,我的NewsItem.swift类中的这段代码也处理图像:

    private func updateDescriptionHTMLImgURL() {
    descriptionHTML = descriptionHTML.stringByReplacingOccurrencesOfString("<img style=", withString: "src=http://")
} 

提前致谢

1 个答案:

答案 0 :(得分:2)

以下代码段将使用正则表达式从字符串中提取任何网址。

var str = // your rss image string
let regex = NSRegularExpression(pattern: "(http://)(.*?)(?=\")", options: NSRegularExpressionOptions.allZeros, error: nil)

var urlString = ""
if let result = regex?.firstMatchInString(str, options: .allZeros, range: NSMakeRange(0, count(str))) {
    urlString = (str as NSString).substringWithRange(result.range)
}

println(urlString) // will print: http://www3.westchestergov.com/images/stories/newsPrimary2015/ferc-logo.jpg

编辑:如何实施

我建议创建一个功能来进行转换。像这样:

func convertRSSImageToURL(rssImage: String) -> NSURL? {
    let regex = NSRegularExpression(pattern: "(http://)(.*?)(?=\")", options: NSRegularExpressionOptions.allZeros, error: nil)

    if let result = regex?.firstMatchInString(rssImage, options: .allZeros, range: NSMakeRange(0, count(rssImage))) {
        return NSURL(string: (rssImage as NSString).substringWithRange(result.range))
    } else {
        return nil
    }
}

然后您可以轻松地将其添加到您的代码中,如下所示:

更改行

} else if let imgURLString = newsItem.imgURL {

} else if let imgURL = convertRSSImageToURL(newsItem.imgURL) {

然后只删除行:

let imgURL = NSURL(string: imgURLString), 

因为您已经打开了NSURL

注意:这没有经过测试,但它应该足以让你自己实现它