views:

28

answers:

1

I'm using NSXMLParser to parse a small XML file containing information about videos. One of the elements in the XML is a URL that points to a thumbnail image associated with each video. I'm trying to parse the XML, get the ImageURL and then use this to fetch the image and store it along with strings such as the video name.

I just can't seem to get it to work. I can see from the debugger that the dictionary has the key/value pair for the image and the image is getting created from the URL, but I'm failing to add it to the NSDictionary. Any suggestions? The following is the fragment of code that deals with the parsing.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{   
    NSLog(@"found this element: %@", elementName);
 currentElement = [elementName copy];
 if ([elementName isEqualToString:@"clipid"]) {
  // clear out our story item caches...
  item = [[NSMutableDictionary alloc] init];
  currentClipID = [[NSMutableString alloc] init];
  currentClipName = [[NSMutableString alloc] init];
  currentImageURL = [[NSMutableString alloc] init];
  currentImage = [[UIImage alloc] init];
 }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
 NSLog(@"found characters: %@", string);
 // save the characters for the current item...
 if ([currentElement isEqualToString:@"clipid"]) {
  [currentClipID appendString:string];
 } else if ([currentElement isEqualToString:@"clipname"]) {
  [currentClipName appendString:string];
 } else if ([currentElement isEqualToString:@"imageurl"]) {
  [currentImageURL appendString:string];
  //Take the Image URL, and convert it to an Image
  NSURL *imageURL = [NSURL URLWithString:string];
  NSData *data = [NSData dataWithContentsOfURL:imageURL];
  UIImage *image = [[UIImage alloc] initWithData:data];
  currentImage = //If it were a string I'd use appendString. How do I add this image to the Dictionary?
 }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
 NSLog(@"ended element: %@", elementName);
 if ([elementName isEqualToString:@"clipid"]) {
  // save values to an item, then store that item into the array...
  [item setObject:currentClipID forKey:@"clipid"];
  [item setObject:currentClipName forKey:@"clipname"];
  [item setObject:currentImageURL forKey:@"imageurl"];
  [item setObject:currentImage forKey:@"image"];
  //videos is an NSMutableArray
  [videos addObject:[item copy]];
 }

}
+1  A: 

You would need to convert the UIImage object to data that can be stored in the NSDictionary.

Try:

NSData *imageData = UIImagePNGRepresentation(image);

Then, add imageData to the dictionary.

Additionally, just add the data from your URL to the NSDictionary.

Evan Mulawski
Lets say I want to use currentImage to hold the NSData from the URL rather than the image itself. How would I need to declare currentImage in didStartElement? How also, would I then add the NSData in a similar way as I use appendString? Thanks.
Phil John
You would need to use NSMutableData in place of NSData. Then, you can use the NSMutableData appendData: method.
Evan Mulawski
I now have currentImage = [[NSMutableData alloc] init]; and I'm using NSURL *imageURL = [NSURL URLWithString:string]; NSData *data = [NSData dataWithContentsOfURL:imageURL]; [currentImage appendData:data]; but the Dictionary is showing 0 bytes in currentImage, even though data is about 12kB. Do I need to change the didEndElement from [item setObject:currentImage forKey:@"image"];
Phil John
Yes, otherwise, every time didEndElement is called, the image is nulled, downloaded, and appended.
Evan Mulawski
Hi Evan. Many thanks for your help so far but you've lost me here. What do I need to change the relevant entry in didEndElement to? I would have thought that [item setObject:currentImage forKey:@"image"]; would do what I wanted, perhaps with some syntax change to account for the fact that currentImage is now NSMutableData.
Phil John
After your last elseif statement in foundCharacters, place another one that check if the current element is equal to string "image." Then place your NSURL and NSData inside of this statement, along with: currentImage = data.
Evan Mulawski
Fantastic. I've finally got it working. I've been stuck on that single problem for hours and hours. Thanks again Evan.
Phil John