首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从url获取图像数据的xml解析。

从url获取图像数据的xml解析。
EN

Stack Overflow用户
提问于 2010-12-28 17:31:04
回答 4查看 1.3K关注 0票数 0

我从事xml解析工作.....

其解析数据完全成功。我需要显示标题,日期,时间和图片。

它在标题,日期,时间,

现在我需要显示图片...

对于图片,它得到了一些url.....

代码语言:javascript
运行
复制
aEventInfo.event_Picture=http://static.djguide.nl/image/flyers/2010/120/91426front.jpg

     NSURL *url3 = [NSURL URLWithString:aEventInfo.event_Picture];
        NSData *data = [NSData dataWithContentsOfURL:url3];
        NSLog(@"%@data",data);
        UIImage *imageView =[[UIImage alloc] initWithData:data];

            CGSize size;
            size.width=400;
            size.height=400;
            UIImage *scaledImage = [imageView scaleToSize:size];
            cell.imageView.image=scaledImage;

我真的喜欢它的崩溃..。如果我有图像url。

如果是aEventInfo.event_picture=nil。它不会崩溃。

如果aEventInfo.event_picture中有任何值..示例http://static.djguide.nl/image/flyers/2010/120/91426front.jpg我的应用程序崩溃了。

我不知道该怎么做..

在这种情况下,有没有人能给我建议和帮助呢?

@提前感谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-12-28 17:51:26

您应该使用连接方法。例如,connectionDidReceive、ConnectionDidFinish。您应该首先创建请求,并使用connection对象建立连接。在此之后,写下连接方法。

票数 1
EN

Stack Overflow用户

发布于 2010-12-28 18:05:32

aEventInfo.event_Picture是一个NSString??你是否正确地初始化了它?因为检索图像的代码看起来没问题,所以您的问题似乎出在为获取URL而传递的字符串上。

它应该是这样的:

代码语言:javascript
运行
复制
NSString* urlString= @"http://static.djguide.nl/image/flyers/2010/120/91426front.jpg";

然后是NSURL *url3 = [NSURL URLWithString:urlString];

票数 0
EN

Stack Overflow用户

发布于 2010-12-28 18:26:57

我尊重Open..source

这是我的代码...//解析类.....

代码语言:javascript
运行
复制
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict{

    if ([elementName isEqualToString:@"events"]) {
        appDelegate.eventListArray=[[NSMutableArray alloc]init];
}
    if ([elementName isEqualToString:@"event"]) {
//      NSString * stringValue = [attributeDict objectForKey:@"id"];
//      [appDelegate.eventListArray addObject:stringValue];
        aEventInfo=[[EventInfo alloc]init];

        aEventInfo.event_ID=[attributeDict objectForKey:@"id"];
        aEventInfo.event_Title=[attributeDict objectForKey:@"title"];
        aEventInfo.event_Description=[attributeDict objectForKey:@"description"];
        aEventInfo.event_Date=[attributeDict objectForKey:@"date"];
        aEventInfo.event_Time=[attributeDict objectForKey:@"time"];
        aEventInfo.event_Location=[attributeDict objectForKey:@"location"];
        aEventInfo.event_Street=[attributeDict objectForKey:@"street"];
        aEventInfo.event_City=[attributeDict objectForKey:@"city"];
        aEventInfo.event_Visitors=[attributeDict objectForKey:@"visitors"];
        aEventInfo.event_Organisation=[attributeDict objectForKey:@"organisation"];
        aEventInfo.event_Price=[attributeDict objectForKey:@"price"];
        aEventInfo.event_Minimum_Age=[attributeDict objectForKey:@"minimum_age"];
        aEventInfo.event_Picture=[attributeDict objectForKey:@"picture"];
        aEventInfo.event_Genre=[attributeDict objectForKey:@"genre"];
        aEventInfo.event_LineUP=[attributeDict objectForKey:@"lineup"];
        aEventInfo.event_WebSite=[attributeDict objectForKey:@"website"];

    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"events"]) {
        NSLog(@"Value of appDelegate is %@",appDelegate.eventListArray);

    }
    if ([elementName isEqualToString:@"event"]) {

        [appDelegate.eventListArray addObject:aEventInfo];
        NSLog(@"appDelegate.eventListArray count %d",[appDelegate.eventListArray count]);
    }
}

//显示数据....在我的表视图中。来自控制器类。- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { //返回段数返回1;}

代码语言:javascript
运行
复制
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
//    return [categorieArray count];
    return [appDelegate.eventListArray count];

}

    // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle= UITableViewCellSelectionStyleBlue;
        cell.backgroundColor=[UIColor blueColor];
        cell.textLabel.textColor=[UIColor redColor];
        cell.detailTextLabel.textColor=[UIColor blackColor];
        cell.detailTextLabel.font=[UIFont systemFontOfSize:10];
    }
    aEventInfo=[[EventInfo alloc] init];
    aEventInfo=[appDelegate.eventListArray objectAtIndex:indexPath.row];
    cell.textLabel.text=aEventInfo.event_Title;
    cell.detailTextLabel.text=[NSString stringWithFormat:@"City: %@\n Date: %@          Time: %@",[aEventInfo event_City],[aEventInfo event_Date],[aEventInfo event_Time]];



    NSURL *url3 = [NSURL URLWithString:aEventInfo.event_Picture];
    NSData *data = [NSData dataWithContentsOfURL:url3];
    NSLog(@"%@data",data);
    UIImage *imageView =[[UIImage alloc] initWithData:data];

    CGSize size;
    size.width=400;
    size.height=400;
    cell.imageView.image=[UIImage imageNamed:imageView];
    return cell;

}

//EventInfo类

代码语言:javascript
运行
复制
@interface EventInfo : NSObject {
    NSString *event_ID;
    NSString *event_Title;
    NSString *event_Description;
    NSString *event_Date;
    NSString *event_Time;
    NSString *event_Location;
    NSString *event_Street;
    NSString *event_City;
    NSString *event_Visitors;
    NSString *event_Organisation;
    NSString *event_Minimum_Age;
    NSString *event_Price;
    NSString *event_Picture;
    NSString *event_Genre;
    NSString *event_LineUP;
    NSString *event_WebSite;
}
@property (nonatomic,retain)NSString *event_ID;
@property (nonatomic,retain)NSString *event_Title;
@property (nonatomic,retain)NSString *event_Description;
@property (nonatomic,retain)NSString *event_Date;
@property (nonatomic,retain)NSString *event_Time;
@property (nonatomic,retain)NSString *event_Location;
@property (nonatomic,retain)NSString *event_Street;
@property (nonatomic,retain)NSString *event_City;
@property (nonatomic,retain)NSString *event_Visitors;
@property (nonatomic,retain)NSString *event_Organisation;
@property (nonatomic,retain)NSString *event_Minimum_Age;
@property (nonatomic,retain)NSString *event_Price;
@property (nonatomic,retain)NSString *event_Picture;
@property (nonatomic,retain)NSString *event_Genre;
@property (nonatomic,retain)NSString *event_LineUP;
@property (nonatomic,retain)NSString *event_WebSite;

@end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4545011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档