0

I want to show the photoUrl images in a CollectionView. How can I do that?

My json data is as shown below:

{
    "albumName": "1",
    "photosCount": 8,
    "photoList": [
        {
            "photoId": 153,
            "photoName": "Photo Description",
            "type": "jpg",
            "size": "15kb",
            "photoURL": "http://1-dot-digiphoto-01.appspot.com/serve?blob-key=AMIfv94-p-RQhFsE4SRRozCxw49YlDIAUWdizg2MQ3h0-YIXXVNXEYVy_ACR7X9qNnATZ1-BW-w5nKk8H-EIpeiMl0766CHadLT6jb4cg225Wv5o2FUBIhaX5oB25l6185HTRvDar9cqz8MmL3WRiIduZMavRDZSXw",
            "dateTaken": "10-Jun-2010 06:11 PM",
            "latitude": "40.000072",
            "longitude": "116.390094"
        },
        {
            "photoId": 154,
            "photoName": "Photo Description",
            "type": "jpg",
            "size": "15kb",
            "photoURL": "http://1-dot-digiphoto-01.appspot.com/serve?blob-key=AMIfv96Vfva45jmoVlR7t0vGGuhKFffMeR_xttn9akPYrdO8Kwce8IRugHl8AeoDyWdgbt7uLv0YR3p2rqdPY1jREpAYyRz7OCHq90Vo8y4vdQ4z8PJLWTtRtqFogbOM_fLop3rfrqMy",
            "dateTaken": "10-Jun-2010 06:11 PM",
            "latitude": "40.000072",
            "longitude": "116.390094"
        }
    ]
}
Infinite Recursion
  • 6,315
  • 28
  • 36
  • 51
Chenna
  • 81
  • 1
  • 9
  • 2
    how far did you try ?? – Huy Nghia Sep 09 '14 at 04:33
  • i have no idea about it please help me – Chenna Sep 09 '14 at 04:36
  • i am sending like this NSDictionary * jsonResult = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil]; albumImageArr = [NSMutableArray array]; [albumImageArr addObjectsFromArray:[jsonResult objectForKey:@"photoList"]]; NSLog(@"Array %@",albumImageArr); NSDictionary * tempDict = [albumImageArr objectAtIndex:0]; str1=[tempDict valueForKey:@"photoURL"]; – Chenna Sep 09 '14 at 04:38
  • 2
    first you should learn about json parsing, then collection views. Its a long process. Believe me, don't expect that someone will code each and everything for you. You should try all the things first by yourself. Then, if you find some issue and problem, people on SO will surely help you – Salman Zaidi Sep 09 '14 at 04:38
  • i am getting only one value in string – Chenna Sep 09 '14 at 04:38
  • a greate tutorial for you http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12 – Huy Nghia Sep 09 '14 at 04:40
  • check out this answer (http://stackoverflow.com/questions/17856055/creating-a-uicollectionview-programmatically) it will show you how to use `UICollectionView` and its delegates. but first you should learn how to parse json. – ChintaN -Maddy- Ramani Sep 09 '14 at 04:41

3 Answers3

0

use AFNetworking with this code...

    //declares arrOfImages as NSMutableArray

    arrOfImages = [[NSMutableArray alloc]init];

    NSString *string =@"Your URL for JSON";

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSDictionary *diction = responseObject;
        NSArray *arr = [diction objectForKey:@"photoList"];
        for (NSDictionary *dict1 in arr) {

            [arrOfImages addObject:[dict1 objectForKey:@"photoURL"]];
        }
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:[NSString stringWithFormat:@"%@",error.localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [hud hide:YES];
        [alert show];
    }];

Now you have NSMutable Array named arrOfImages with the lilst of URL. now you can set that URL to the Image View of Collection View.

Brainstorm Technolabs
  • 1,204
  • 1
  • 17
  • 30
0

Check out this answer Creating a UICollectionView programmatically you have to modify few things as below.

in .h

NSMutableArray *arrObject;

in .m when you got response in dictionary.

if([yourDictionary[@"photoList"] isKindOfClass:[NSArray Class]])
{
    arrObject = yourDictionary[@"photoList"];
    [yourCollectionview reloadData];
}

then your delegates will be called . use (https://github.com/rs/SDWebImage) to download image async.

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return arrObject.count;
}
    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //create your cell and pass url
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    [yourcell.imageView sd_setImagewithUrl:[arrObject[0] valueForKey:@"photoURL"]];
    return cell;
}
Community
  • 1
  • 1
ChintaN -Maddy- Ramani
  • 5,006
  • 1
  • 24
  • 46
0

Is your question for Objective-C code or Swift code? What you pasted is one JSON object with an array called photoList as one of its contents. What you need to do is get photoList.

Objective-C sample code is as follows:

Assuming data is the JSON file and NSJSONSerialization has been imported. jsonDictionary is a global variable declared as a NSDictionary while photoList is a global variable declared as a NSArray.

In your viewDidLoad:

jsonDictionary = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers];

photoList = [jsonDictionary objectForKey:@"photoList"];

In cellForItemAtIndexPath method:

NSString *path = [[self.photoList objectAtIndex:indexPath.row] objectForKey:@"photoURL"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];


cell.imageView.image = image;

Hope this helps...

Koh
  • 1,570
  • 1
  • 8
  • 6
  • and i want to show these images in next detailed view. – Chenna Sep 09 '14 at 06:37
  • - (void)collectionView:(UICollectionView *)collectionView1 didSelectItemAtIndexPath:(NSIndexPath *)indexPath { UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; PhotoDescriptionViewController *albumsController = (PhotoDescriptionViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"DescriptionView"]; NSString *senderImg=[arr objectAtIndex:indexPath.item]; albumsController.receivedImage=senderImg; [self.navigationController pushViewController:albumsController animated:YES]; } – Chenna Sep 09 '14 at 07:01
  • i am sending like this but in next view it shows null – Chenna Sep 09 '14 at 07:01
  • You are passing in a string (senderImg) to what I believe is a UIImage object (albumsController.receivedImage). You must use the string to get a NSURL, and from there get an NSData to build a UIImage. – Koh Sep 09 '14 at 18:17