的Xcode与解析XML与图像
我编码XML解析器读取网页上的图片,但我有一个问题,当我编译我的Xcode的文件 他说:“线程1:SIGABRT的Xcode与解析XML与图像
下面是代码:
查看或者Controller.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<NSXMLParserDelegate> {
IBOutlet UIImageView *imgView;
NSMutableArray *photos;
}
@end
viewController.m:
#import "ViewController.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidLoad
{
[super viewDidLoad];
photos = [[NSMutableArray alloc] init];
NSXMLParser *photoParser = [[[NSXMLParser alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://davylebeaugoss.free.fr/Sans%20titre.xml"]] autorelease];
[photoParser setDelegate:self];
[photoParser parse];
NSURL *imageURL = [NSURL URLWithString:[photos objectAtIndex:0]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
[imgView setImage:image];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"photo"])
{
[photos addObject:[attributeDict objectForKey:@"url"]];
}
}
@end
提前谢谢!
NSURL *imageURL = [NSURL URLWithString:[photos objectAtIndex:0]];
是你的问题。
[photoParser parse];
是异步的,这意味着当调用objectAtIndex:0
时,它不是完整的。
在引用数组之前,您需要等到解析完成。解析完成时,方法
- (void)parserDidEndDocument:(NSXMLParser *)parser
将被调用。拨打电话以参考photo
阵列。
- (void)viewDidLoad
{
[super viewDidLoad];
photos = [[NSMutableArray alloc] init];
NSXMLParser *photoParser = [[[NSXMLParser alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://davylebeaugoss.free.fr/Sans%20titre.xml"]] autorelease];
[photoParser setDelegate:self];
[photoParser parse];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSURL *imageURL = [NSURL URLWithString:[photos objectAtIndex:0]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
[imgView setImage:image];
}
我加解决方案所需的更改。 – 2013-03-17 13:21:07
我完全按照你的说法做了,但我仍然有同样的问题...总是出现同样的错误。我指的是“appdelegate.h” – 2013-03-17 16:53:29
这里没有问题:P只是一个说法,你得到一个崩溃的地方 – 2013-03-17 01:07:01
在这行是错误当您运行......因为这是一个信号,它在编译的时候心不是BTW – 2013-03-17 01:10:04