将 RSS 提要中的缩略图图像显示到 tableView 单元格中

DIsplaying a tumbnail image from an RSS feed into tableView cell

我正在为工作创建一个 RSS 提要应用程序,我几乎完成了大部分工作,除了一件事,将每篇文章的图像加载到缩略图 UIImage 视图中。我在网上搜索了一些答案,但几天后仍然没有弄明白。这是我到目前为止与图像相关的代码:

在我的 MWFeedParser.m class 中:

else if ([currentPath isEqualToString:@"/rss/channel/item/media:thumbnail"])
{
    if ([currentElementAttributes objectForKey:@"url"])
        {
            item.thumbnailURL = [currentElementAttributes valueForKey:@"url"];
                            processed = YES;
        } 
}

在我的 RSSMaster.m class 中:

 NSString *imageURLstr = item.thumbnailURL ? item.thumbnailURL : @""; 

在我的 RSSResults.m class:

NSLog(@"IMAGE URL: %@", imageURLstr);

if (![imageURLstr isEqualToString:@""]) { 
NSURL *imageURL = [NSURL URLWithString:imageURLstr];

//image download
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
/* Fetch the image from the server... */
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
/* This is the main thread again, where we set the tableView's image to
be what we just fetched. */
cell.thumbnail.image = img;
        });
    });
} 

图像从 src= 的代码开始,后面是文章描述。所以我的问题是,如何对我的项目进行编码,以便将每篇文章的单个图像 link 拉入我的 tableView 单元格中的缩略图 UIImage 视图中?

如果能提供详细的帮助,我将不胜感激,这样我才能最终完成这项工作,谢谢!

You can use an XML Parser,或者您可以将其视为 NSString 并像这样提取 src 属性:

NSString *imageURLstr=item.summary;
imgURL = [imgURL substringToIndex:[imgURL rangeOfString:@"alt="].location-2];
imgURL = [imgURL substringFromIndex:[imgURL rangeOfString:@"src="].location+[@"src=" length]+1];

NSLog(@"src: %@",imgURL);