1

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"];
Viraj
  • 1,880
  • 22
  • 31
kingston
  • 1,553
  • 4
  • 22
  • 42

2 Answers2

1

Use Lazy Loading to load images....

Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • @mehta:dude i appreciate...but dis stuff looks a little complex i m a noob....is it possibly to speed up dis stuff any other way using nsxmlparser – kingston Mar 10 '11 at 05:36
  • Your code is ok to download the images.... you just need to download particular images in background thread using same code.. so that your table scrolls smoothly... sorry but you need to give little time to this...another thing you need to release memory also which you are allocating in didStartElement but that is not related – Mihir Mehta Mar 10 '11 at 05:45
  • could u help me out on which part of the above code ... i exactly got to use the thread – kingston Mar 10 '11 at 05:53
  • 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]; – Mihir Mehta Mar 10 '11 at 06:07
  • It is not that simple... you need to refer that code on how to do that in uitableview – Mihir Mehta Mar 10 '11 at 06:10
  • i was working with another project ..where clicking a particular table view cell(without image on table view) takes u to another view....where it loads the flicer rss images.. i had dis problem where clicking the table view cell..freezes the screen...takes probably 60sec to load next view where the rss images get loaded......thanks for the thread u mentioned...i was able to move to the next screen whout freezing but takes time for the image to get loaded on the next view.....cud u help me wid dis..anyways thanks – kingston Mar 10 '11 at 06:58
  • just click the transparent tik mark arrow just below left side of my answer... just below the < 0 > (vertical) – Mihir Mehta Mar 10 '11 at 08:27
0

somewhat dated but should put you in the right direction

http://kosmaczewski.net/2009/03/08/asynchronous-loading-of-images-in-a-uitableview/

Jason Bugs Adams
  • 737
  • 1
  • 5
  • 11
  • he is using NSXMLParser. see line 54 [https://github.com/akosma/async-uitableview/blob/master/Classes/Helpers/RSS.m](https://github.com/akosma/async-uitableview/blob/master/Classes/Helpers/RSS.m) – Jason Bugs Adams Mar 10 '11 at 09:25