-3

i have code to display a single image from json array data in a UIImageView. i want to display all images in multiple UIImageViews. plz suggest me how to loop images to display all. json array data link:

Here's code from my app

 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];
madLokesh
  • 1,860
  • 23
  • 49
PavanAlapati
  • 41
  • 2
  • 8

3 Answers3

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];
mahesh chowdary
  • 432
  • 5
  • 13
  • 1
    Could you explain what in the code you have posted is an answer to the problem happening here, so that the user and future readers can learn from it? – Andrew Barber Apr 15 '14 at 18:27
0

After parsing JSON response use follow code

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];

*Note: Now you are going with Synchronous Request to download & load images in the view. It is not a good approach. Use Asynchronous requests for downloading images and loading them.

Find the following URL for asynchronous image downloading

Asynchronous downloading of images for UITableView with GCD

Community
  • 1
  • 1
Satya
  • 3,320
  • 1
  • 20
  • 19
0

you can get an image dictionary into array like this, into data object Suppose i have PostClubDC class with posDC obj

 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];
        }
Noor
  • 2,071
  • 19
  • 28