UITableview,从rss feed加载图像

时间:2011-03-10 04:53:40

标签: objective-c xcode uitableview nsxml

我正在从rss feed中读取xml图像并在UITable视图中解析它。一切正常,但在表格视图中加载图像内容需要时间。屏幕仍然冻结。我正在使用NSXMLParser来解析图像。你能帮助我吗,我真的很有意思。以下是代码。

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

2 个答案:

答案 0 :(得分:1)

使用Lazy Loading加载图片....

答案 1 :(得分:0)

有点过时但应该让你朝着正确的方向前进

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