从 ios 中的 Url 获取多个图像
Posted
技术标签:
【中文标题】从 ios 中的 Url 获取多个图像【英文标题】:Geting multiple Images from Url In ios 【发布时间】:2014-03-06 06:02:58 【问题描述】:我有代码在 UIImageView 中显示来自 json 数组数据的单个图像。 我想在多个 UIImageViews 中显示所有图像。 请建议我如何循环显示所有图像。 json数组数据链接:
这是我的应用程序中的代码
NSString *urlString=[NSString stringWithFormat:@"http://ielmo.xtreemhost.com/array.php"];
NSURL * url=[NSURL URLWithString:urlString];
NSData *data =[NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableArray *json=(NSMutableArray*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%@",[json objectAtIndex:0]);
NSString *imageString=[NSString stringWithFormat:@"%@",[json objectAtIndex:0]];
NSURL * urlone=[NSURL URLWithString:imageString];
NSData *datanew =[NSData dataWithContentsOfURL:urlone];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[imageView setImage:[UIImage imageWithData:datanew]];
testView = [[UIView alloc] initWithFrame:CGRectMake(30.0f, 300.0f, 300.0f, 200.0f)];
[testView addSubview:imageView];
[self.view addSubview:testView];
【问题讨论】:
【参考方案1】:NSMutableArray *arr=[[NSMutableArray alloc] init];
NSString *urlString=[NSString stringWithFormat:@"http://ielmo.xtreemhost.com/array.php"];
NSURL * url=[NSURL URLWithString:urlString];
NSData *data =[NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableArray *json=(NSMutableArray*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%d",[json count]);
NSLog(@"%@",[json objectAtIndex:0]);
NSString *imageString;
for (int i=0; i<[json count]; i++)
imageString=[NSString stringWithFormat:@"%@",[json objectAtIndex:i]];
[arr addObject:imageString];
NSLog(@"%@",arr);
// NSString *imageString=[NSString stringWithFormat:@"%@",[json objectAtIndex:0]];
NSURL * urlone=[NSURL URLWithString:imageString];
NSData *datanew =[NSData dataWithContentsOfURL:urlone];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[imageView setImage:[UIImage imageWithData:datanew]];
UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(30.0f, 300.0f, 300.0f, 200.0f)];
[testView addSubview:imageView];
[self.view addSubview:testView];
【讨论】:
您能否解释一下您发布的代码中出现的问题的答案,以便用户和未来的读者可以从中学习?【参考方案2】:解析 JSON 响应后使用跟随代码
UIScrollView *testView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,480)];
for(int i=0; i<[json count]; i++)
NSLog(@"%@",[json objectAtIndex:i]);
NSString *imageString=[NSString stringWithFormat:@"%@",[json objectAtIndex:i]];
NSURL * urlone=[NSURL URLWithString:imageString];
NSData *datanew =[NSData dataWithContentsOfURL:urlone];*
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,i*480,320,480)];
[imageView setImage:[UIImage imageWithData:datanew]];
[testView addSubview:imageView];
[self.view addSubView:testView];
*注意:现在您将使用同步请求在视图中下载和加载图像。这不是一个好方法。使用异步请求来下载和加载图像。
找到以下网址进行异步图片下载
Asynchronous downloading of images for UITableView with GCD
【讨论】:
【参考方案3】:您可以像这样将图像字典放入数组中,放入数据对象中 假设我有带有 posDC obj 的 PostClubDC 类
for(NSDictionary *returnDicto in dataArray)
PostClubDC *postDC = [[PostClubDC alloc] init];
NSDictionary * returnDict = [returnDicto objectForKey:@"club_info"] ;
postDC.postID = [[returnDict objectForKey:@"Id"]integerValue];
postDC.postName = [returnDict objectForKey:@"name"];
postDC.postImage = [returnDict objectForKey:@"image"];
[yourDataArray addObject:postDC];
【讨论】:
以上是关于从 ios 中的 Url 获取多个图像的主要内容,如果未能解决你的问题,请参考以下文章