【问题标题】:Nothing displayed after splitting a list of image URLs拆分图像 URL 列表后不显示任何内容
【发布时间】:2011-10-20 14:26:16
【问题描述】:

我在一个名为 aux 的字符串中有六个图像 URL。我用换行符分隔的组件拆分它。我想从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];
}

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

【问题讨论】:

  • @Vladimer 对此的任何解决方案
  • @Vladimir 解决我的问题
  • 尝试调试您的代码并检查您在字符串中实际得到的值

标签: objective-c uiimage nsdata nsurl


【解决方案1】:

如果 dataimage 都不是 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;

【讨论】:

  • 您是否获得了正确的 URL 并加载了所有数据?确保您在某处添加了此 imageView(我认为您已完成此操作)作为子视图。但这真的很奇怪,我不明白为什么它不应该工作。
  • 你能显示listItems中存储了哪些数据吗?而且我不明白这个添加了什么:aux = [_homeText.text stringByAppendingString:[NSString stringWithFormat:@" %@ ",item.shortDescription]]; [_homeText setText:aux]; 到那个 aux 字符串。
  • 我已经展示了如何用换行符分割行。你能说出 listItems 中有多少元素吗?
  • 我知道如何分割换行元素,但如何获取这些元素并在图像视图中显示
  • 天哪,对不起,我刚刚注意到出了什么问题。您尝试在NSURL *url = [NSURL URLWithString:aux]; 中使用一大堆URL。您应该改用NSURL *url = [NSURL URLWithString:[listItems objectAtIndex:0]];。要一次显示所有六张图片,您需要 6 个不同的 imageView 并使用图片的 URL 遍历数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多