ios - 如何从 xml 提要上传 ios rss 提要表格单元格的缩略图

标签 ios xml xcode rss uitableview

我为工作需要创建的 rss 应用程序即将完成。我对 xcode 还是很陌生,我已经尝试了很多不同的开源代码。我终于找到了一个更适合我需要的,但我最不需要的是让缩略图图像适用于表格 View 单元格。单元格中的文章标题和描述加载正常,但当我尝试重新编写图像代码时,它出现空白。

该项目是为了与谷歌新闻 rss 一起工作:https://news.google.com/?output=rss .我使用的 rss 是:http://www3.westchestergov.com/home/all-press-releases?format=feed&type=rss

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

    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,这工作正常,当我搜索他们的 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 提要一起工作吗?这是我的最后一 block 拼图,如果我能让它工作,我终于可以把这个应用程序抛在脑后了。

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

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

提前致谢

最佳答案

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

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

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

关于ios - 如何从 xml 提要上传 ios rss 提要表格单元格的缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32097651/

相关文章:

python - 如何使用 "&"字符设置属性值

xml - 阅读和理解 R 中的 XML

ios - 如何在 xcode 8.3.2 中查看下载的配置文件?

ios - Swift 不允许我有 2 个同名选择器

ios - 从泛型方法接收参数

iphone - 将字符串从一个 View 复制到 iphone 中的另一个 View

iphone - 在 iOS 中绘制 NSAttributedString?

xml - 使用 XSL 转换标签序列

xcode - 错误 'Unsupported Configuration' 是什么意思?

ios - 使用 SVProgressHUD 无法在 Xcode 中构建