我在名为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];
}
没有错误,但我没有图像。为什么?我该如何解决这个问题?
答案 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;