1

我有一个视图控制器,上面有一个表格视图。我需要从 news.yahoo.com/rss/ 解析新闻标题和相应的图像。截至目前,我能够解析新闻标题,但无法获取图像。xml 的 url 是:view-source: http://news.yahoo.com/rss/

到目前为止的代码如下:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    //if element name is equal to item then only i am assigning memory to the NSObject class

    if([elementName isEqualToString:@"item"]){
        xmlStringFileObject =[[XMLStringFile alloc]init];
    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    //whatever data i am getting from node i am appending it to the nodecontent variable
    [nodecontent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    NSLog(@"node content = %@",nodecontent);
}

//bellow delegate method specify when it encounter end tag of specific that tag

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    //I am saving my nodecontent data inside the property of XMLString File class
/*  if([elementName isEqualToString:@"channel"])
    {
        xmlStringFileObject.xmlChannelTitle = nodecontent;

    } */
    if([elementName isEqualToString:@"title"]){
        xmlStringFileObject.xmltitle=nodecontent;
    }
    /*else if([elementName isEqualToString:@"media:Content"]){
        xmlStringFileObject.xmllink=nodecontent;
    } */

    //finally when we reaches the end of tag i am adding data inside the NSMutableArray
    if([elementName isEqualToString:@"item"]){

        [rssOutputData addObject:xmlStringFileObject];
        [xmlStringFileObject release];
        xmlStringFileObject = nil;
    }
    //release the data from mutable string variable
    [nodecontent release];

    //reallocate the memory to get new content data from file
    nodecontent=[[NSMutableString alloc]init];
}





// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    rssOutputData = [[NSMutableArray alloc]init];

    //declare the object of allocated variable
    NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://news.yahoo.com/rss/"]];

    //allocate memory for parser as well as 
    xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
    [xmlParserObject setDelegate:self];

    //asking the xmlparser object to beggin with its parsing
    [xmlParserObject parse];

    //releasing the object of NSData as a part of memory management
    [xmlData release];
}

表格查看方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return[rssOutputData count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @"MyIdentifier";

    // Try to retrieve from the table view a now-unused cell with the given identifier
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];


    // If no cell is available, create a new one using the given identifier
    if (cell == nil) {
        //cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];

        //add some extra text on table cell .........
        cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle   reuseIdentifier:MyIdentifier] autorelease];

    }
    // Set up the cell
    CGRect imageFrame = CGRectMake(2, 8, 40, 40);
   self.customImage = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease];

    self.customImage.image = [UIImage imageNamed:@"AppIcon.png"];//dummy image   
    [cell.contentView addSubview:self.customImage];

    CGRect contentFrame = CGRectMake(45, 2, 265, 30);
    UILabel *contentLabel = [[[UILabel alloc] initWithFrame:contentFrame] autorelease];
    contentLabel.numberOfLines = 2;
    contentLabel.font = [UIFont boldSystemFontOfSize:12];
    contentLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
    [cell.contentView addSubview:contentLabel]; 

    //cell.detailTextLabel.text=[[rssOutputData objectAtIndex:indexPath.row]xmllink];

    return cell;
}

我应该怎么做才能从 media:content 中获取图像?(标签-> 的一个例子<media:content url="http://l3.yimg.com/bt/api/res/1.2/jf1WLvOXmBeLdow9IgIu3Q--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/bcfc8d0780ca8e0e300f6a7067004e25.jpg" type="image/jpeg" width="130" height="86"></media:content>)有人可以帮忙吗?

4

1 回答 1

0

Download the image which is referenced in the "media:content url" with imageWithContentsOfURL:.

E.g.:

NSURL *imageURL = [NSURL URLWithString:__THE_URL_FROM_THE_media-content_TAG__];
self.customImage.image = [UIImage imageWithContentsOfURL:imageURL];
于 2013-05-07T15:05:39.650 回答