I'm pulling data into a UITableView via an XML feed from a Wordpress site. I wanted to display the table with an image if the post contained one and a default image if it did not. So in my
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
method, I have an if statement that looks like this:
if ([elementname isEqualToString:@"content:encoded"]) {
NSString *firstImageURL = [self getFirstImageUrl:currentStory.content];
currentStory.imageURL = firstImageURL;
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString: firstImageURL]]];
currentStory.image = image;
}
This calls getFirstImageURL, which looks like this:
-(NSString *)getFirstImageUrl: (NSString *) html {
NSScanner *theScanner;
NSString *imageURL = nil;
theScanner = [NSScanner scannerWithString: html];
// find start of tag
[theScanner scanUpToString: @"<img" intoString: NULL];
if ([theScanner isAtEnd] == NO) {
[theScanner scanUpToString: @"src=\"" intoString: NULL];
NSInteger newLoc2 = [theScanner scanLocation] + 5;
[theScanner setScanLocation: newLoc2];
// find end of tag
[theScanner scanUpToString: @"\"" intoString: &imageURL];
}
return imageURL;
}
Everything works as it should, but loading the table takes about 5 to 6 seconds and can sometimes take up to 10 seconds, which is not desirable. I was wondering if there is anything I can do to speed up the process of grabbing the first photo.
UPDATE
So after more investigation, it appears that the bottleneck I'm seeing has nothing to do with me downloading images. In fact the actual downloading of images takes no longer than 2 seconds consistently. It looks like the bottleneck happens when I download the RSS feed:
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
This consistently takes the longest.
2012-03-30 14:35:11.506 gbllc[883:3203] inside grabDataForFeed
2012-03-30 14:35:11.510 gbllc[883:3203] reached loadXMLByURL
2012-03-30 14:35:11.512 gbllc[883:3203] after stories alloc
**** 5 seconds ****
2012-03-30 14:35:16.568 gbllc[883:3203] after initWithContentsOfURL
2012-03-30 14:35:16.570 gbllc[883:3203] after initWithData
2012-03-30 14:35:16.573 gbllc[883:3203] about to parse
*** I now parse the XML and download images, takes 2 seconds ***
2012-03-30 14:35:18.066 gbllc[883:3203] Parsed successfully
Right after I alloc my data object, I grab the data for parsing. So I guess my original question is no longer valid and I should probably ask if there's a faster way to grab the initial data for parsing or if I should change my model and try to use json or something?