UITable 视图,从 rss feed 加载图像

发布于 2024-10-21 18:30:34 字数 3293 浏览 2 评论 0原文

我正在从 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"];

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咿呀咿呀哟 2024-10-28 18:30:34

使用 延迟加载 加载图像...

Use Lazy Loading to load images....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文