[英]How do I display images from my parsed XML in my iOS RSS app?
I have an iOS app in Swift that parses XML from an RSS feed from my website.我在 Swift 中有一个 iOS 应用程序,它从我网站的 RSS 提要中解析 XML。
I want to be able to display the posts' image and also displaying the relevant content (as at the moment it just displays XML code).我希望能够显示帖子的图像并显示相关内容(因为目前它只显示 XML 代码)。
Here is the code to the Parser and the cells it controls:这是解析器及其控制的单元格的代码:
Parser:解析器:
import Foundation
struct RSSItem {
var title: String
var description: String
var pubDate: String
}
// download xml from a server
// parse xml to foundation objects
// call back
class FeedParser: NSObject, XMLParserDelegate
{
private var rssItems: [RSSItem] = []
private var currentElement = ""
private var currentTitle: String = "" {
didSet {
currentTitle = currentTitle.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}
private var currentDescription: String = "" {
didSet {
currentDescription = currentDescription.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}
private var currentPubDate: String = "" {
didSet {
currentPubDate = currentPubDate.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
}
private var parserCompletionHandler: (([RSSItem]) -> Void)?
func parseFeed(url: String, completionHandler: (([RSSItem]) -> Void)?)
{
self.parserCompletionHandler = completionHandler
let request = URLRequest(url: URL(string: url)!)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: request) { (data, response, error) in
guard let data = data else {
if let error = error {
print(error.localizedDescription)
}
return
}
/// parse our xml data
let parser = XMLParser(data: data)
parser.delegate = self
parser.parse()
}
task.resume()
}
// MARK: - XML Parser Delegate
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:])
{
currentElement = elementName
if currentElement == "item" {
currentTitle = ""
currentDescription = ""
currentPubDate = ""
}
}
func parser(_ parser: XMLParser, foundCharacters string: String)
{
switch currentElement {
case "title": currentTitle += string
case "description" : currentDescription += string
case "pubDate" : currentPubDate += string
default: break
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if elementName == "item" {
let rssItem = RSSItem(title: currentTitle, description: currentDescription, pubDate: currentPubDate)
self.rssItems.append(rssItem)
}
}
func parserDidEndDocument(_ parser: XMLParser) {
parserCompletionHandler?(rssItems)
}
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error)
{
print(parseError.localizedDescription)
}
}
Cell View Controller:单元格视图控制器:
import UIKit
class NewsTableViewController: UITableViewController
{
private var rssItems: [RSSItem]?
private var cellStates: [CellState]?
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 155.0
tableView.rowHeight = UITableView.automaticDimension
fetchData()
}
private func fetchData()
{
let feedParser = FeedParser()
feedParser.parseFeed(url: "https://appleosophy.com/feed") { (rssItems) in
self.rssItems = rssItems
self.cellStates = Array(repeating: .collapsed, count: rssItems.count)
OperationQueue.main.addOperation {
self.tableView.reloadSections(IndexSet(integer: 0), with: .left)
}
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// Return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
guard let rssItems = rssItems else {
return 0
}
// rssItems
return rssItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NewsTableViewCell
if let item = rssItems?[indexPath.item] {
cell.item = item
cell.selectionStyle = .none
if let cellStates = cellStates {
cell.descriptionLabel.numberOfLines = (cellStates[indexPath.row] == .expanded) ? 0 : 4
}
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
tableView.deselectRow(at: indexPath, animated: true)
let cell = tableView.cellForRow(at: indexPath) as! NewsTableViewCell
tableView.beginUpdates()
cell.descriptionLabel.numberOfLines = (cell.descriptionLabel.numberOfLines == 0) ? 3 : 0
cellStates?[indexPath.row] = (cell.descriptionLabel.numberOfLines == 0) ? .expanded : .collapsed
tableView.endUpdates()
}
}
My objective is to have a fully native RSS feed into my app that can display my articles with images and the text attributed to the article.我的目标是在我的应用程序中添加一个完全原生的 RSS 提要,该提要可以显示我的文章以及图片和文章的文本。
This answer is quite late, but I'm working on a similar project.这个答案已经很晚了,但我正在做一个类似的项目。 I am using SwiftUI and managed to load the remote URLs.
我正在使用 SwiftUI 并设法加载远程 URL。 Hopefully my solution will be helpful to you:
希望我的解决方案对您有所帮助:
First you load the image首先加载图像
import Combine
import Foundation
class ImageLoader: ObservableObject {
var dataPublisher = PassthroughSubject<Data, Never>()
var data = Data() {
didSet {
dataPublisher.send(data)
}
}
init(urlString:String) {
guard let url = URL(string: urlString) else { return }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
DispatchQueue.main.async {
self.data = data
}
}
task.resume()
}
}
Then you create an imageView:然后你创建一个 imageView:
import Combine
import SwiftUI
struct ImageView: View {
@ObservedObject var imageLoader:ImageLoader
@State var image:UIImage = UIImage()
init(withURL url:String) {
imageLoader = ImageLoader(urlString:url)
}
var body: some View {
VStack {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width:100, height:100)
}.onReceive(imageLoader.dataPublisher) { data in
self.image = UIImage(data: data) ?? UIImage()
}
}
}
struct ImageView_Previews: PreviewProvider {
static var previews: some View {
ImageView(withURL: "")
}
}
Then you can use your ImageView to see the image: ImageView(withURL: data.imageURL)
然后你可以使用你的 ImageView 查看图像:
ImageView(withURL: data.imageURL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.