2011-03-10 148 views
1

我讀取rss提要的XML圖像並解析它在UITable視圖。一切工作正常,但它需要時間來加載表格視圖中的圖像內容。屏幕仍然凍結。我正在使用NSXMLParser解析圖像。你們能幫我一把嗎,我真的很貪婪。以下是代碼。UITable視圖,從rss feed加載圖像

- (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"]; 

回答

1

使用Lazy Loading載入圖像....

+0

@mehta:哥們我欣賞...但DIS東西看起來有點複雜的ima noob ....是否可能使用nsxmlparser以任何其他方式加速dis東西 – kingston 2011-03-10 05:36:31

+0

您的代碼可以下載圖像....您只需要使用相同的代碼在後臺線程中下載特定圖像。讓你的桌子滾動順利...對不起,但你需要給點時間這...你需要釋放內存也是你在didStartElement中分配的內存,但這是不相關的 – 2011-03-10 05:45:57

+0

可以幫助我在上面的代碼的哪一部分。 ..我完全可以使用線程 – kingston 2011-03-10 05:53:48

0
+0

下面是可能的wid nsxmlparser ...無論如何謝謝 – kingston 2011-03-10 07:19:11

+0

他是usin g NSXMLParser。見第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) – 2011-03-10 09:25:36

+0

謝謝...我會試一試 – kingston 2011-03-10 09:39:35