【问题标题】:UITable view,loading images from rss feedUITableview,从 RSS 提要加载图像
【发布时间】:2011-03-10 04:53:39
【问题描述】:

我正在从 rss 提要读取 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"];

【问题讨论】:

    标签: objective-c xcode uitableview nsxml


    【解决方案1】:

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

    【讨论】:

    • @mehta:dude 我很感激......但是我是个菜鸟,dis 的东西看起来有点复杂......是否有可能使用 nsxmlparser 以任何其他方式加速 dis 的东西
    • 您的代码可以下载图像....您只需要使用相同的代码在后台线程中下载特定图像..以便您的表格平滑滚动...抱歉,但您需要给出时间不多了...您还需要释放您在 didStartElement 中分配的内存的另一件事,但这不相关
    • 你能帮我看看上面代码的哪一部分......我确实要使用线程
    • NSString *imagefile1 = [[stories objectAtIndex:indexPath.row]objectForKey:@"url"]; NSString *escapedURL=[imagefile1 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; UIImage *image1 = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:escapedURL]]];单元格.imageView.image=image1; [image1 发布];
    • 没那么简单……你需要参考那个代码来了解如何在uitableview中做到这一点
    【解决方案2】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    相关资源
    最近更新 更多