6

我是 iPhone 的新手开发人员。我正在做简单的 xml 解析演示。我想我怎样才能得到遵循 xml 代码的图像???

<item> 
      <title>Rain washes out England qualifier</title>  
      <description>England's World Cup qualifier against Poland is postponed because of a waterlogged pitch and is rescheduled for 16:00 BST on Wednesday.</description>  
      <link>http://www.bbc.co.uk/sport/0/football/19941036</link>  
      <guid isPermaLink="false">http://www.bbc.co.uk/sport/0/football/19941036</guid>  
      <pubDate>Tue, 16 Oct 2012 21:18:26 GMT</pubDate>  
      <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/63530000/jpg/_63530879_016250328-1.jpg"/>  
      <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/63529000/jpg/_63529179_016250328-1.jpg"/> 
    </item>  

我怎样才能得到图像?

我已经使用以下代码获得了标题或描述。

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

    if([elementName  isEqualToString:className])
    {
        [items addObject:item];
        [item release];
        item = nil;
    }
    else
    {
        if([currentNodeContent length] != 0)
        {

            if([className  isEqualToString:@"item"])
            {


                if([elementName  isEqualToString:@"title"]|| [elementName  isEqualToString:@"description"] || [elementName isEqualToString:@"url"])
                {
                    [item setValue:currentNodeContent forKey:currentNodeName];

                    if ([elementName isEqualToString:@"media:thumbnail"])
                    {
                         [item setValue:currentNodeContent forKey:@"url"];
                    }

                }


                else
                {

                }

            }
        }
            else
            {
            }

        }


    [currentNodeContent release];
    currentNodeContent = nil;
    [currentNodeName release];
    currentNodeName = nil;

}

提前致谢。

4

3 回答 3

0

要从 url 获取图像,请使用此方法返回图像

UIImage * imageFromURL = [self getImageFromURL:imageurlstring];

-(UIImage *) getImageFromURL:(NSString *)fileURL {
  UIImage * result;

  NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
  result = [UIImage imageWithData:data];

  return result;
}

要保存图像,请使用此方法

[self saveImage:imageFromURL withFileName:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1] inDirectory:documentsDirectoryPath];

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
   imageName = [imageName stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
   [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, extension]]options:NSAtomicWrite error:nil];       
}
于 2012-10-17T06:12:42.540 回答
0

您可以将 .h 文件中的 imageLink 声明为 NSString。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI  qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
  if([elementName isEqualToString:@"media:thumbnail"]) {
    NSLog(@"attributes1: %@", attributeDict);
    imageLink = [attributeDict objectForKey:@"url"];
  }
}

使用上面的代码,您可以获取图像 url,然后使用以下代码显示图像。

NSData* imageData = [NSData dataWithContentsOfURL:[NSURL urlWithString:imageLink]];
UIImage* image = [UIImage imageWithData:imageData];
于 2012-10-17T06:22:53.833 回答
0

您的 xml 在服务器上只有一个图像的 url,因此在解析时将此 url 保存在字符串中。当您需要在屏幕上显示图像时,请使用从 url 获取图像

NSData* imageData = [NSData dataWithContentsOfURL:[NSURL urlWithString:your_url]];
UIImage* image = [UIImage imageWithData:imageData];

要异步加载图像,Git Hub 上有很多可用的示例,请使用 asyncImage 之一,它会缓存最近下载的图像。

于 2012-10-17T06:08:30.603 回答