【问题标题】:xml parsing to fetch image data from the urlxml解析从url获取图片数据
【发布时间】:2010-12-28 09:31:04
【问题描述】:

我正在做 xml 解析.....

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

它适用于标题、日期、时间,

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

图片获取一些网址.....

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;

我确实喜欢它的崩溃...如果我有图片网址。

如果一个EventInfo.event_picture=nil。 它没有崩溃。

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

我不知道该怎么做..

谁能给我建议 并在这种情况下帮助我。

@提前谢谢。

【问题讨论】:

  • 使用JonLOo方法创建带字符串的url

标签: iphone


【解决方案1】:

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

【讨论】:

  • @Raxit...你有这个连接方法的例子吗?
  • 你可以在这里找到示例代码developer.apple.com/library/ios/navigation/…。示例代码,例如 SimpleURLConnections、AdvancedURLConnections、URLCache。这很有帮助。
  • @Raxit 非常感谢它为我工作。 NSURL *url = [NSURL URLWithString:aEventInfo.event_Picture]; NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url] returnedResponse:nil error:nil]; imageView.image = [UIImage imageWithData:data];
【解决方案2】:

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

应该是这样的:

NSString* urlString= @"http://static.djguide.nl/image/flyers/2010/120/91426front.jpg";

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

【讨论】:

  • 是的,它是一个 string.NSString。并在解析时正确初始化。我得到一个EventInfo.event_Picture。值,这意味着我的初始化完美完成。
  • 你能在这里发布崩溃吗?以及您解析 aEventInfo.event_Picture 的方式
  • -[UIImage 长度]:无法识别的选择器发送到实例 0x736f840 2010-12-28 16:00:18.052 PartyTemperature [715:207] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因: '-[UIImage length]: unrecognized selector sent to instance 0x736f840' *** call stack at first throw:
  • 这告诉您您正在尝试调用 length 方法,它是 UIImage 实例中的 NSString 方法,因此您似乎正在某处将字符串初始化为 UIImage
【解决方案3】:

我尊重开源..source

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

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

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

}

//事件信息类

@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

【讨论】:

  • 然后我 @synthesize 我的 EventInfo 中的每个对象。类
  • 尝试在 eventInf.event_picture 之前进行强制转换:NSURL *url3 = [NSURL URLWithString:(NSString *)aEventInfo.event_Picture];
  • 你能尝试像我之前告诉你的那样引入字符串而不进行解析,只是为了测试它是否正确检索数据? NSString* urlString= @"static.djguide.nl/image/flyers/2010/120/91426front.jpg";
  • 我试试这个代码...NSString *urlimage=@"static.djguide.nl/image/flyers/2010/120/90511front.jpg"; NSURL *url3 = [NSURL URLWithString:urlimage]; NSData *data = [NSData dataWithContentsOfURL:url3]; NSLog(@"%@data",data); UIImage *imageView =[[UIImage alloc] initWithData:data]; CGSize 大小;尺寸.宽度=400;尺寸.高度=400; UIImage *scaledImage = [imageView scaleToSize:size]; cell.imageView.image=scaledImage;返回单元格; ...它只是显示数据而不是图像
  • 你为什么要创建一个新的 UIIMage?尝试使用 cell.imageView.image= [imageView scaleToSize:size];
【解决方案4】:

您是否尝试过使用 TouchXML。借助 TouchXML,XML 解析变得非常容易使用。我在使用 NSXMLParser 时遇到了同样的问题。切换到 TouchXML,XML 解析变得更容易了。

查看link

【讨论】:

  • 。我正在使用 NSXMLParser ......我开始学习解析 iphone
  • 尝试使用 TouchXML。它非常易于使用。您只需按照链接中提供的步骤操作即可。甚至我还处于 iPhone 编程的初级阶段。
猜你喜欢
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多