iPhone RSS Reader, get image from rss feed - iphone

i've a problem.
I've an app that download rss feed from a website. It works all fine, but i want to set image in table view where the articles appear. How can i get image from RSS Feed? I need the code.
For title, description i've done it perfectly, but i'm not able to recover image.
P.S. : sorry for my english, i'm italian :)

Ok, so I presume you have the URL of the image. You can do this:
//Normal text setting code
//Normal text setting code
NSString *imageURLString = [rssFeed objectForKey:#img"];
NSURL *imageURL = [NSURL URLWithString:imageURLString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
cell.imageView.image = [UIImage imageWithData:imageData];
That is the quick and filthy method. It's running in your UI thread etc, so it's going to be a terrible user experience. You can fiddle with performance on your own terms, but I'd suggest one of the following:
Load all your data asynchronously and fill the data as it becomes available
Load all of your data in a secondary thread, but show a loading spinner
For both of the above, you would put your images into an NSArray and then call them by doing cell.imageView.image = [myImageArray objectAtIndex:indexPath.row];
The best solution however (and most complicated) though is called 'lazy loading'.
If you want to do lazy loading (the way facebook and twitter do it) you should check this out: LazyTableImages.
Happy coding, Zane

There is a plugin for it.
WP RSS Images Plugin
http://wordpress.org/extend/plugins/wp-rss-images/
After downloading it and succesfully installing it you need to parse the enclosure tag.
It will be in enclosure element with the attribute of "url". If you need I can post the code also.

Related

Downloading image from server without file extension (NSData to UIImage)

From my server I'm pulling down a url that is supposed to simply be a profile image. The relevant code for pulling down the image from the urls is this:
NSString *urlString = [NSString stringWithFormat:#"%#%#",kBaseURL,profile_image_url];
profilePic = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
My url is in the format (note no file extension on the end since its dynamically rendered)
localhost:8000/people/1/profile_image
If I load the url in my browser the image displays; however the code above for pulling down the UIImage does not work. I've verified that the code does pull an image from a random site on the interwebs.
Any thoughts on why this is happening?
Does it work if you feed NSURL
http://localhost:8000/people/1/profile_image
I imagine that the http:// is important here (so that the NSURL knows whether it's pointing at a file or remote URL).
The reason you can't display it in a UIImageView is because the UIImageView doesn't know what format it's in (jpeg,png,etc.)
A good way for future coding is always use extensions. Don't take short cuts, they will always end up worse and you'll have to fix them over and over again.
First check for the following :
Permissions: change from read only to read and write.
I have tried many methods in my project to display pic from server and the most prominent issue i have found is that by default, your localhost folders are read only and you need to change permission as well.... Initially i thought read only wudnt matter if i am just loading the picture beacuse technically i am only reading the contents , right. But it did matter.
White Spaces
One more thing you coud check for is the name of your image containing white spaces, if they are then you need to remove white spaces from your image name while making URL request.
Try adding extensions if nothing works out
If eveythings fine , try to load the image with extensions which would obviously work.
Provide us with some initial coding as well
Let us know about your code snippets and console outputs which could help the community to reach the solution of the problem in a more prominent manner.

Load static google map into UIImageView?

I'm trying to show a small static google map in my app. I'm wondering if it's possible to do this using UIImageView? It doesn't seem to be working for me but maybe I'm doing something wrong.
This is what I have (I stripped the URL so that it's easier to read):
NSURL *mapurl = [[NSURL alloc] initWithString:#"http://maps.google.com/maps/api/staticmap?[...]"];
NSData *mapdata = [[NSData alloc] initWithContentsOfURL:mapurl];
UIImage *uimap = [[UIImage alloc] initWithData:mapdata];
self.map.image = uimap; // map is my UIImageView
[mapurl release];
[mapdata release];
[uimap release];
I know my UIImageView is setup correctly because if I replace the URL with one of an image then it loads fine. Also I know the google maps URL is correct because if I load it straight into a browser it shows up fine.
I'm starting to think that this might not be possible. If anyone has any ideas or alternatives please share.
Thanks.
Well I ended up finding a solution for this so I'll post it in case someone else runs into the same issue.
First I changed the way I load the image to using NSURLConnection. I don't think this particular change makes a difference but this is what allowed me to find the problem. When trying to load the URL I got a "bad url" error. So I did some searching on that and turns out there are some invalid characters in the google static map URL that must be escaped.
So basically just call this on the url and you should be good to go:
// The URL you want to load (snipped for readability)
NSString *mapurl = #"http://maps.google.com/maps/api/staticmap?[...]";
// Escape the string
mapurl = [mapurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
I haven't tested this solution with the code I have in my original post but I did test it with my new NSURLConnection implementation and it works perfectly. I assume it'll work fine with the code above as well since it was just a matter of escaping the string.

iPhone UIWebView slow loading to local HTML files

I'm developing an app that requires caching web pages (completely) along with their CSS files and images after saving the entire HTML of the page (going through the links to store each file along with the HTML file).
While viewing the HTML file offline, UIWebView takes a long time to load the page, given that I'm already offline, and the file is on disk along with its CSS and images.
I'm using this code to load the file:
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFilePath];
[wView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL fileURLWithPath:self.htmlFolderPath isDirectory:YES]];
Is there any other means of loading the file to the UIWebView that can load it faster?
P.S: it loads very fast on simulator (offline) but on device it takes a long time (considering its an already offline cached file)
Thanks for help.
Is your solution actually cache things other than the html file, meaning pictures, css, etc, AND relinking them in the html page? I am guessing that you are downloading and caching the html page and external resources then the UIWebView is loading that html and still going out to load the external resources. That would explain the fast performance on the simulator and the slower performance on device.
Here is a library designed to do exactly what you are trying to do: ASIWebPageRequest.
It should also be noted that it could simply be a case of disk i/o bottlenecking. I know on my project, I was loading large images in a uitableview and even after they were cached I noticed quite a bit of lag when pulling them off the disk because they were so big.
I've found certain kinds of CSS can grind WebView rendering to a halt. For example:
body { -webkit-box-shadow:inset 0 0 100px #222; }
This works great in the simulator, looks nice too. But on the phone (even the iPhone 4, iOS 4.2), a simple page will take 10sec to render.
Since this probably just takes time because it needs to parse and render the page, you may consider firing up the UIWebView in the background; i.e. added as a subview, but not visible.
Maybe the UIWebView is smart enough to know it doesn't need to do anything, but I suspect that at least html and css parsing is done right away.
If it doesn't do anything without being visible, reduce size to 1x1 and set opacity=0, and put that pixel some place where it can't interfere with touch event handling.
Not perfect but it may work.
I'm almost sure you will never be able to do this. The UIWebView just needs some time to process your webpage even when it's a local page.
Keeping that in mind you can try to preload the page before it's being shown. For example if you show it after a user presses a button, preload the page when you show the button instead of when the user actually presses the button. The user doesn't notice the slow loading, because it's being handled in the background so when the user presses the button the page is already been loaded.
I can give you an idea about alternative ways of loading HTML from file into the UIWebView. A small project I've got uses Objective-C as a pure wrapper for UIWebView
The project is here, but the main code is below: http://github.com/robb1e/iWeb
- (void)viewDidLoad {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html" inDirectory:#"."]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[super viewDidLoad];
}
I'm also exploring ways of improving the perceived performance by showing an image while the DOM is getting ready. Some answers I've had are here:
Displaying a background image on UIWebView
Just if in case someone happens the same to me...
I had a UIWebView that was loading a string html and every resource (js and css) was stored locally.
I've faced that loading the content with internet connection it was some kind of slow (1 or 2 seconds to load and appear the webview in my controller) but when I tried to load the same page WITHOUT internet connection it was fast, really fast.
I've remembered that my HTML template had this in the start <base href="http://somesite.com" /> that I've used to load some images whit relative paths in some contents.
Removing that work as charm. So that can be making your web view load slower even if you don't have any reference to extern content in your HTML.
Even in my case, while loading local html files to webview was taking too much time.
Try to load local html files as below, it worked for me:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"website_and_mobile_tnc-1" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[_TandCView loadHTMLString:[headerString stringByAppendingString:htmlString] baseURL:nil];
If you want to load using NSData, try to make baseUrl to "nil" in your code.
I have modified your code as below,
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFilePath];
[wView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:nil];
I didn't tried this, but please try and let me know.

Getting Error While sending an image From an iPhone emil using MailComposer

I am using MailComposer available in iPhone sdk3.0.
I am using the following code to add image using html to my email body. It displays the image properly in mail body. But when I try to send it. It breaks leaving message in the log , No image found. Can anyone help me regarding this where am going wrong. Like I also tried attaching the images, but it wasnt working. It does display the images in the composer but breaks while sending.
UIImage *tempImg = [self getSmallImage:[dbPersistenceObj getImage:[[imageIdArray objectAtIndex:i] intValue]]];
NSData *imageData = UIImageJPEGRepresentation(tempImg,1);
NSString *encodedImage = [self base64EncodingWithLineLength:[imageData length] data:imageData];
emailBody = [emailBody stringByAppendingFormat:#"<b><img src='data:image/jpg;base64,%#' alt='64 bit image'/></b>", encodedImage];
tnx.
data: URLs are not supported when sending email. Upload the image to a central server and then link to that using a standard <img src="http://example.com/image.jpg" /> tag
Just tested with 3.1 - works like a charm!
"data: "URLs are definitely supported in 3.1 when sending email with the MFMailComposeViewController!

Is that possible to cache UIWebView in iPhone?

I was using NYTimes iPhone application, I become bit curious when I notice that it cache UIwebview even though I didn't open that article.Does anyone have idea how to do that?
How NYTimes iPhone application doing offline reading?
Any help greatly appreciated.
Thanks,
Amit
I wrote the NYTimes app. Here are some details that you could have gotten by looking inside the app bundle.
I download the HTML for the articles, strip out whatever unsupported HTML and JS crud the producers stuffed in it and cache it in the backing store.
The article content is contained in a series of P tags (HTML fragment). I stuff that into a special HTML skeleton page that ships with the app. The static wrapper page also contains CSS and JS used to properly display the article and lazily load the images.
The image loading is really cool. The web view is notified when the images are ready. layout is not affected because I already know the sizes of the missing images.
You can use UIWebView to load html files stored locally on the iPhone.
NYTimes app is probably caching html and images in local storage.
Search google for "UIWebView local" and you get several useful hits.
I tried it out and it works great:
First, create a "view based application" and add a UIWebView to the NIB.
Second, add this code to your UIViewController code:
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[super viewDidLoad];
}
Third, add a file called "index.html" to your "Resources" folder in xcode and it will be displayed.
UPDATE:
Indeed, the complicated part of this is downloading the images and stylesheets for the webpage. Doing this server side is easy with Simple HTML Parser (and PHP). Just package everything in a zip and download to your iPhone.
Alternatively, you could do it locally with a C/C++/OBJC HTML parser (libxml2.2 is available on iOS). See this SO question Parsing HTML on the iPhone.
It's going to a bit of a project, so good luck.