hope someone can help. I appreciate there are a few questions that are similar on here, but not specific enough for me.
I'm teaching myself iOS, so decided to write a simple RSS reader app from a Wordpress site. All is working fine, but now I want to try and store the page within iOS to read when offline. Thought it would be a good way to learn how to encode and store data.
Reading the XML nodes and storing title, description, etc is all fine, that works. The issue I've got is when there's an image embedded in the feed. If I read and store the 'content:encoded' XML info, then view that in a webView that works fine, unless I'm offline when everything will work except for the image, as that's just a HTML reference.
I guess I want to
a) identify the image URL, then remove it from the content:encoded b) download and store in my array, associated with the post c) then read the image back out again.
Or something with a different approach, but similar effect.
If there wa开发者_StackOverflow中文版s a way of adding a new XML 'featuredimage' node or similar, this would be relatively straightforward I think, but I can't see how to do that.
Would be very grateful for any ideas/pointers on this one, many thanks.
I would suggest a slightly different approach, which I think should make things easier:
- identify the image:
you can use any RegEx library for iOS to parse your content:encoded
info;
- download the image:
use ASIHTTPRequest or NSUURLRequest to download the image and store it locally;
- modify the
content:encoded
info:
modify the <img>
tag so that its src
attribute is set to the locally store image (instead of the external one); you can do this by using the same approach as in point 1. In other words, say you have an image like:
`<img src="http://xxxx.yyy.zzzz/image.png" />`
you can change this to:
`<img src="image.png" />`
- appropriately specify
baseURL
when loading the HTML into the UIWebView:
if you specify a baseURL
when calling the loadHTMLString:baseURL:
selector, any "local" uri will be resolved against the baseURL you specify. If you decide to store the image in the document directory, then you can do this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[_label loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:documentsDirectory]];
so that any image like:
`<img src="localImage.png" />`
will be retrieved locally.
Hope this helps.
精彩评论