我有代码在UIImageView中显示来自json数组数据的单个图像。 我想在多个UIImageViews中显示所有图像。 PLZ建议我如何循环图像显示所有。 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];
答案 0 :(得分:0)
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];
答案 1 :(得分:0)
解析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];
*注意:现在您要使用同步请求下载&amp;在视图中加载图像。这不是一个好方法。使用异步请求下载图像并加载它们。
找到异步图像下载的以下URL
答案 2 :(得分:0)
你可以将像这样的数组中的图像字典导入到数据对象中 假设我有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];
}