分割图像URL列表后未显示任何内容

时间:2011-08-05 06:21:09

标签: objective-c uiimage nsdata nsurl

我在名为aux的字符串中有六个图像网址。我用分隔换行符的组件拆分它。我想从aux逐个选择图片,并在ImageView中显示每个图片。我的代码是:

aux = [_homeText.text stringByAppendingString:[NSString stringWithFormat:@" %@  ",item.shortDescription]];
[_homeText setText:aux];  
NSString *list =aux;
NSArray *listItems = [list componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
[listItems count];
if ([listItems objectAtIndex:0]) {
        NSURL *url = [NSURL URLWithString:aux];
            /*UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"hai" message:aux delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
                [alert show];
                [alert release];*/          
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [[UIImage alloc] initWithData:data]; 
        [_homeImage setImage:image];
}
else if ([listItems objectAtIndex:1])
{
            NSURL *url = [NSURL URLWithString:aux];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *image = [[UIImage alloc] initWithData:data]; 
            [_homeImage setImage:image];
}

没有错误,但我没有图像。为什么?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果数据图像不是 nil ,您可以尝试正确初始化_homeImage。 我不知道为什么,但我认为你创建的_ homeImage 是这样的:

_homeImage = [[UIImageView alloc] init];

尝试做这样的事情:

CGSize imageSize = [image size];
[_homeImage setFrame:CGRectMake([_homeImage frame].origin.x, 
                                [_homeImage frame].origin.y, 
                                imageSize.width, imageSize.height)];
[_homeImage setImage:image];
[image release], image = nil;