从服务器获取图像的问题

时间:2011-04-22 05:21:57

标签: iphone objective-c ipad

我正在尝试从网址获取图片。有人指出我错在哪里是我的代码吗?

NSString *filesContent = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.projects-demo.com/iphone/xml/Menu.xml"] ];

    DDXMLDocument *ddDoc = [[DDXMLDocument alloc] initWithXMLString:filesContent options:0 error:nil];
    DDXMLElement *ddMenu = [ddDoc rootElement];
    NSArray *ddChildren = [ddMenu children];

    for (int j = 0 ;j < [ddChildren count]; j++) {
        DDXMLElement *image1st = [[ddMenu elementsForName:[NSString stringWithFormat:@"cookingType%d",j+1]] objectAtIndex:0];
        for (DDXMLNode *n in [image1st children]) {
        //  if ([[n name] isEqualToString: @"cookingType"]) {
                MenuModel *model = [[MenuModel alloc] init];

                NSLog(@"image of cooking........%@",[n stringValue]);
                model.imgsrc = [n stringValue];
                [listofimages addObject:model];
                //ss
                //======
                NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:model.imgsrc]];
            NSLog(@"printing    my data ....",mydata);
                UIImage *myimage = [[UIImage alloc] initWithData:mydata];

我试图打印nsDAta但它什么都没有。

1 个答案:

答案 0 :(得分:1)

只是观察一下,你的变量myData的NSLog错过了%@,不确定这只是一个复制粘贴错误还是HTML没有显示的内容。

同时尝试登录[myData length]可能会出现下载问题。

最后,我建议您异步执行所有URL调用。

看起来有点像这样

` - (void)loadingThumnailFormURL:(NSString *)thumbnailURL {

[imageData release];
imageData = [[NSMutableData alloc] init];

NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:thumbnailURL]];
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];

[urlRequest release];
[urlConnection start];

}`

毋庸置疑,您必须为NSURLConnection实现委托方法并捕获数据。