Reputation: 1619
I'm trying to get image from RSS Feed with MWFeedParser. I'm getting feed items, but when I was try to reach image, something is missing in content
value. It has to be there, but it's not.
So I changed the content value with link. Link is like that:
Then now match
value is getting nil. I don't figured out what is the problem.
This is my RSS Feed url what I'm use to :
let url = NSURL(string: "feed://www.uefa.com/rssfeed/uefaeuro/rss.xml")
This is my code to get image from 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
}
Upvotes: 0
Views: 283
Reputation: 3003
Try to set image with URLRequest instead, as shown below:
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
Image is tiny in the screenshot but that is due to my imageview config. I am guessing your imageview is configured to fit bigger image.
Good Luck!
***Note: The code above is for Swift 3.
Upvotes: 1