How to display image from a RSS feed

梦想与她 提交于 2019-12-06 08:52:14

In answer to the question in the title, to load an image, you parse the RSS feed for the URL of the string using NSXMLParser object, and when you want to present that image to the user, you use that URL to retrieve the image into a NSData, create a UIImage for that, and then set the image property of the UIImageView accordingly. See the cellForRowAtIndexPath in the listing below.

In terms of the specifics of the implementation, I've significantly revised this answer on the basis of the fact that you're reading a feed from http://news.yahoo.com/rss/ which has a format significantly different than what you show in your example XML listing. The format actually appears to be:

<item>
  <title>Palestinian death toll ...</title>
  <description>this contains the html version of the story description</description>
  <link>http://news.yahoo.com/palestinian-death-toll-...</link>
  <pubDate>Mon, 19 Nov 2012 11:20:15 -0500</pubDate>
  <source url="http://www.reuters.com/">Reuters</source>
  <guid isPermaLink="false">palestinian-death-toll-...</guid>
  <media:content url="http://the.url.for.image/images/palestinian.JPG" type="image/jpeg" width="130" height="86"></media:content>
  <media:text type="html">another html version of the text</media:text>
  <media:credit role="publishing company"></media:credit>
</item>

Some of this is just standard XML tags title, description, etc. where you're grabbing values between the open and close tags (for which you can just use NSXMLParserDelegate method parser:foundCharacters:). But the image URL is actually inside the url attribute of the media:content tag, itself, in which case you have to use the NSXMLParserDelegate method didStartElement and inquire on the attributes parameter.

So, given that, I've uploaded a sample Yahoo News reader which demonstrates the process of parsing the Yahoo RSS feed. Note, this is a fairly primitive implementation, but hopefully this gives you the basic idea.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!