且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从xml获取图像?

更新时间:2023-01-18 19:12:21

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

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