6

I am a Fresher Developer in iPhone . i am working on simple xml parse demo. I want just how can i get the image that following xml code ???

<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>  

how can i get images ?

i already get title or description using following code.

- (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;

}

Thanks in advance.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
The Rock
  • 147
  • 5
  • NSData* imageData = [NSData dataWithContentsOfURL:[NSURL urlWithString:imageLink]]; UIImage* image = [UIImage imageWithData:imageData]; put above code in NSXMLParser, for URL element – Kamleshwar Oct 17 '12 at 07:06
  • But this will slower speed of parsing if there will be long list of images or images are heavy in size(That is not recommended) – Kamleshwar Oct 17 '12 at 07:07

3 Answers3

0

Your xml has just a url of your image at server, so while parsing save this url in a string. And at the time you require to show the image on screen, get the image from the url using

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

To load the images asynchronously there are lots of samples available on Git Hub, use the asyncImage one, which caches the recently downloaded images.

vishy
  • 3,241
  • 1
  • 20
  • 25
0

To get the image from the url use this method it returns an image

UIImage * imageFromURL = [self getImageFromURL:imageurlstring];

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

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

  return result;
}

To save the Image use this method

[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];       
}
Kjuly
  • 34,476
  • 22
  • 104
  • 118
vamsi575kg
  • 594
  • 1
  • 7
  • 23
0

You can declare imageLink in .h file as 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"];
  }
}

Using above code you can get image url and then use following code to display image.

NSData* imageData = [NSData dataWithContentsOfURL:[NSURL urlWithString:imageLink]];
UIImage* image = [UIImage imageWithData:imageData];
Kjuly
  • 34,476
  • 22
  • 104
  • 118
Prasad G
  • 6,702
  • 7
  • 42
  • 65