私は NSXMLParser を使用して、ビデオに関する情報を含む小さな XML ファイルを解析しています。XML の要素の 1 つは、各ビデオに関連付けられたサムネイル画像を指す 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]];
}
}