I m reading the xml images from rss feed and parsing it in UITable view. Everything works fine, but it takes time to load the image content in the table view. The screen remains frozen. I'm using NSXMLParser to parse the image. Could you guys help me out, I'd be really greateful. Below is the code.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName copy];
currentElement1=[attributeDict copy];
if ([elementName isEqualToString:#"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentString=[[NSMutableString alloc] init];
//currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([attributeDict objectForKey:#"url"])
{
currentString=[attributeDict objectForKey:#"url"];
// NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"url"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentContent forKey:#"content:encoded"];
[item setObject:currentDate forKey:#"pubDate"];
[stories addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...///////////element
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"pubDate"]) {
[currentDate appendString:string];
}
else if ([currentElement isEqualToString:#"content:encoded"]) {
[currentSummary appendString:string];
}
}
NSString *imagefile1 = [[stories objectAtIndex:indexPath.row]objectForKey:#"url"];
NSString *escapedURL=[imagefile1 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
UIImage *image1 = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:escapedURL]]];
cell.imageView.image=image1;
[image1 release];
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.numberOfLines=2;
cell.textLabel.text=[[stories objectAtIndex:indexPath.row] objectForKey: #"title"];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.numberOfLines=3;
cell.detailTextLabel.text=[[stories objectAtIndex:indexPath.row] objectForKey: #"pubDate"];
Use Lazy Loading to load images....
somewhat dated but should put you in the right direction
http://kosmaczewski.net/2009/03/08/asynchronous-loading-of-images-in-a-uitableview/
Related
I am having some difficulty dealing with memory leaks in the following code.
Using the leaks instrument within XCode, which shows up the memory leaks within some of my code that is used for rss parsing.
I am using XCode 4, and releasing the allocations at the foot of the code. I have tried adding releases to each local section which causes crashes or the program to stop working.
Any help of advice much appreciated!!
The code which causes the leaks:
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:#"Unable to download story feed from web site (Error code %i )", [parseError code]];
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"]) {
item = [[NSMutableDictionary alloc] init];
currentImage = [[NSMutableString alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentImage forKey:#"media"];
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"summary"];
[item setObject:currentDate forKey:#"date"];
[stories addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([currentElement isEqualToString:#"media"]) {
[currentImage appendString:string];
} else if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"pubDate"]) {
[currentDate appendString:string];
}
}
And the releasing later on:
- (void)dealloc {
[currentElement release];
[rssParser release];
[stories release];
[item release];
[currentImage release];
[currentTitle release];
[currentDate release];
[currentSummary release];
[currentLink release];
[super dealloc];
}
Change the following:
UIAlertView * errorAlert = [[[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
This code should be executed only once:
item = [[NSMutableDictionary alloc] init];
currentImage = [[NSMutableString alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
At the start of each didStartElement: method new instances are boing created yet only releases once when the class is dealloc'ed. Thus, as suing that didStartElement: is called more than once there is a build-up of instances of the string objects.
Probably what you want is to create these instances once at the instantiation of the class and then append to them as elements are encountered.
In any event release for each allocation.
i m parsing an rss feed and loading in a webview...wat i want is ..to place a custom activity indicator in the exact place..where the parsing begins and the place where the parsing ends....below is the code.
#implementation MenuAndWineListViewController
NSDictionary *dict;
UIAlertView * errorAlert;
- (void)viewDidLoad
{
self.title=#"Menu & WineList";
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
activityIndicator1 = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]autorelease];
activityIndicator1.frame=CGRectMake(0.0,0.0, 40.0, 40.0);
activityIndicator1.center=self.view.center;
[self.view addSubview:activityIndicator1];
NSURL *baseURL=[[NSURL
URLWithString:#"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com /?feedpages&max=0&sort_order=ASC&parent=12&child_of=12"]retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connection1=[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
if ([stories count] == 0) {
path = #"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com/?feedpages&max=0&
sort_order=ASC&parent=180&child_of=180";
[self parseXMLFileAtURL:path];
}
[menuAndWineListViewController loadHTMLString:[dict objectForKey:#"description"] baseURL:nil];
[menuAndWineListViewController setClipsToBounds:YES];
menuAndWineListViewController.opaque=NO;
menuAndWineListViewController.backgroundColor=[UIColor clearColor];
[menuAndWineListViewController setDelegate:self];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(#"found file and started parsing");
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
stories = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"])
{
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"item"])
{
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentSummary forKey:#"description"];
[stories addObject:[item copy]];
}
for (i=0 ; i<stories.count;i++)
{
dict = [stories objectAtIndex:i];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentElement isEqualToString:#"title"])
{
[currentTitle appendString:string];
}
else if ([currentElement isEqualToString:#"description"])
{
[currentSummary appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[activityIndicator1 stopAnimating];
[activityIndicator1 removeFromSuperview];
NSLog(#"stories array has %d items", [stories count]);
}
I'd put the
[activityIndicator1 startAnimating];
at the beginning of the parseXMLFileAtURL method
and the
[activityIndicator1 stopanimating];
at the beginning of the parserDidEndDocument like you did.
how to add an image on the table view header....where the image is read from rss feed and stored in the array called item below is the code
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 69.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* headerView = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 320.0, 69.0)];
headerView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: #"1" ofType: #"jpg"]]];
return headerView;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName copy];
currentElement1=[attributeDict copy];
item = [[NSMutableDictionary alloc] init];
if ([elementName isEqualToString:#"item"]) {
// clear out our story item caches...
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentString=[[NSMutableString alloc] init];
currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([elementName isEqualToString:#"enclosure"])
{
currentString=[attributeDict objectForKey:#"url"];
// NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"url"];
}
if ([elementName isEqualToString:#"itunes:image"])
{
currentImage = [attributeDict objectForKey:#"href"];
[item setObject:currentImage forKey:#"href"];
// NSLog(#"the item current string:%#",item);
NSString *imagefile1 = [item objectForKey:#"href"];
NSString *escapedURL=[imagefile1 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
UIImage *image1 = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:escapedURL]]];
NSLog(#"here we go dis is awesome:%#",image1);
//cell.imageView.image=image1;
image.image=image1;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(#"ended element: %#", elementName);
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentContent forKey:#"content:encoded"];
[item setObject:currentDate forKey:#"pubDate"];
[stories addObject:[item copy]];
}
}
parse XML
Download Image to disk
load Image into ImageView
add imageView to headerView
Each of this step is well documented on SO, developer.apple.com and elsewhere.
i m using nsxmlparser to read the apple itunes rss feed..could u guys help to read this particular xml image.
<im:image height="55">http://a1.phobos.apple.com/us/r1000/028/Music/5c/aa/fe/mzi.fsnbyjmf.55x55-70.jpg</im:image>
below is the code
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary*) attributeDict
{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"entry"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentString=[[NSMutableString alloc] init];
currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([attributeDict objectForKey:#"href"])
{
currentString=[attributeDict objectForKey:#"href"];
NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"href"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"entry"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentDate forKey:#"published"];
//[item setObject:currentImage forKey:#"im:image height=55"];
NSLog(#"the current image content:%#",item);
[stories addObject:[item copy]];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...///////////element
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"published"]) {
[currentDate appendString:string];
}
else if ([currentElement isEqualToString:#"url"]) {
[currentContent appendString:string];
}
}
#user652878 try this coding its works well..... In didStartelement, write as follows.. else if([elementName isEqualToString:#"media:content"])
{
currentImage = [attributeDict valueForKey:#"url"];
}
In didEndElement, ....... else if([elementName isEqualToString:#"media:content"]){
[item setObject:currentImage forKey:#"image"];
}
In foundcharacters........................ else if([currentImage isEqualToString:#"media:content"])
{
[currentImage appendString:string];
}
use NSLog to see that we get image link or not.... Sure u will get ur solution...
I need help for parsing the image using dictionary. i can get the text data using dictionary but can't parse the image of the corresponding content using dictionary here my code
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"title"]) {
item = [[NSMutableDictionary alloc] init];
ktitle = [[NSMutableString alloc] init];
kpubdate = [[NSMutableString alloc] init];
kitem = [[NSMutableString alloc] init];
//kmedia = [[UIImageView alloc] init];
klink = [[NSMutableString alloc] init];
kdescription = [[NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"title"]) {
[item setObject:ktitle forKey:#"title"];
[item setObject:klink forKey:#"link"];
[item setObject:kdescription forKey:#"description"];
[item setObject:kpubdate forKey:#"pubDate"];
//kmedia = [[UIImageView alloc] initWithImage:[UIImage keyPathsForValuesAffectingValueForKey:#"media:content"]];
//[item setObject:kmedia forKey:#"media:content"];
[arrayList addObject:[item copy]];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if([currentElement isEqualToString:#"title"]){
[ktitle appendString:string];
}
else if ([currentElement isEqualToString:#"link"]) {
[klink appendString:string];
}
else if([currentElement isEqualToString:#"description"]){
[kdescription appendString:string];
}
}
finally i found answer small mistake is I parse the tag instead of url
Thanks for the help