【问题标题】:How do I retrieve a image from a rss entry?如何从 rss 条目中检索图像?
【发布时间】:2013-02-28 11:59:39
【问题描述】:

我目前正在开发一个新闻应用程序,并且非常想将图像添加到新闻文章图像的表格行中。

我已经设法解析了其他所有内容,只是我无法解析图像 url。

这是我用于从提要中剥离 HTML 的代码。

.M 文件

- (NSString *)stringByStrippingHTML:(NSString *)inputString 
{
NSMutableString *outString;

if (inputString)
{
    outString = [[NSMutableString alloc] initWithString:inputString];

    if ([inputString length] > 0)
    {
        NSRange r;

        while ((r = [outString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        {
            [outString deleteCharactersInRange:r];
        }      
    }
}

return outString; 

}

.H 文件

- (NSString *)stringByStrippingHTML:(NSString *)inputString;

我对 xcode 和一般代码都很陌生,所以我对这些东西并不熟悉。

我只是无法解析图片的网址。

这是表格行外观的代码。

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.numberOfLines=3;
cell.detailTextLabel.numberOfLines=4;


RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];

NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];

NSString * articleDescrptionString = [[[NSString alloc] init] autorelease];
articleDescrptionString = entry.articleDescription;

cell.textLabel.text = entry.articleTitle; 

NSString *plainString = [self stringByStrippingHTML:entry.articleDescription ];

cell.imageView.image = [UIImage imageNamed: entry.articleImageUrl];

//NSString *rangedString = [plainString substringToIndex:249];  //0 to 249 makes it 250 characters

//cell.detailTextLabel.text = articleDescrptionString;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, plainString];

//UIImage *theImage = [UIImage imageWithContentsOfFile:entry.articleImageUrl];
//cell.imageView.image = theImage;

return cell;
}

我注释掉的任何代码目前都没有使用。

【问题讨论】:

    标签: uitableview ios5 xcode4.2 tablerow


    【解决方案1】:

    您需要先将图像加载到 NSData 对象中...

    NSURL *imgURL = [NSURL URLWithString:entry.articleImageUrl];
    NSData *imgData = [NSData dataWithContentsOfURL:imgURL];
    cell.imageView.image = [UIImage imageWithData:imgData];
    

    但是您必须异步执行,因为如果您在没有延迟图像加载的情况下执行此操作,您的 TableView 将在滚动时存储...

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多