I have an Objective C application that reads data from an RSS feed using ASIHTTPRequest and parses it using GDataXML. There are two pieces of the RSS data returned that I’d like to display in a table. Each item returned in the RSS feed has a image URL and a description. I’d like to populate the table with the image in one cell and the description in the next cell to the right of the image. My current logic is looping over each item returned from the RSS feed and populating a UIImage object using the following logic:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.example.com/image.png"]]];
cell.image = [UIImage imageWithData: image];
This works, but seems to run very slowly. Sometimes the application just freezes while trying to load the pictures. Is there a more optimal way to load the images in the table? If I ignore the images, the table populates with the text data quickly.
In the above line of code,
NSURL URLWithString:is to use with file system URL like already downloaded image in your temp directory or plist file etc. Since you have passed ahttpURL it makes synchronous request blocking your thread until it finishes downloading image data. And if you are doing this on main thread then UI doesn’t respond to touches until image gets downloaded.Change it to make asynchronous call, i.e., download example.com/image.png asynchronously. You may refer to
ASIHTTPRequestinstance methodstartAsynchronousand then pass downloaded data toUIImage imageWithData:To learn more about asynchronous network requests, watch session 208 WWDC 2010 Network Apps for iPhone OS Part-2