0

我正在使用 NSXMLParser 解析一个包含视频信息的小型 XML 文件。XML 中的元素之一是指向与每个视频关联的缩略图的 URL。我正在尝试解析 XML,获取 ImageURL,然后使用它来获取图像并将其与字符串(例如视频名称)一起存储。

我似乎无法让它工作。我可以从调试器中看到字典具有图像的键/值对,并且图像是从 URL 创建的,但我无法将其添加到 NSDictionary。有什么建议么?以下是处理解析的代码片段。

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

}
4

1 回答 1

4

您需要将 UIImage 对象转换为可以存储在 NSDictionary 中的数据。

尝试:

NSData *imageData = UIImagePNGRepresentation(image);

然后,将 imageData 添加到字典中。

此外,只需将 URL 中的数据添加到 NSDictionary。

于 2010-10-22T13:27:26.560 回答