PavanAlapati
PavanAlapati

Reputation: 39

how to put the JSON data into UICollectionView in ios

I try to get the Images from a JSON URL and place on UICollectionView

but i don't how to get images from JSON

 #import "ViewController.h"
    #import "CustomCell.h"
    @interface ViewController ()
    {

        NSArray *arrayOfImages;
        NSArray *arrayOfDescriptions;
        NSMutableArray *json;
        NSString *img;
        NSMutableData *webData;
         NSURLConnection *connection;
    }
    @end


    @implementation ViewController

    - (void)viewDidLoad
    {
        /**/
        [[self myCollectionView]setDataSource:self];
        [[self myCollectionView]setDelegate:self];

        NSString *urlString=[NSString stringWithFormat:@"http://ielmo.xtreemhost.com/array.php"];


        NSURL * url=[NSURL URLWithString:urlString];
        NSURLRequest *req=[NSURLRequest requestWithURL:url];

        connection=[NSURLConnection connectionWithRequest:req delegate:self];

        if(connection)
        {
            NSLog(@"Connected");

            webData=[[NSMutableData alloc]init];
        }
  [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength:0];

}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Error is");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{



    NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];

    NSLog(@"all array isn %@",al);




    NSData* myData = [NSKeyedArchiver archivedDataWithRootObject:al];

    NSLog(@"Data is data%@",myData);

   NSError *error;

    json=(NSMutableArray*)[NSJSONSerialization JSONObjectWithData:myData options:kNilOptions error:&error];
    NSLog(@"Data is arrayjson%@",json);

    for(int i=0;i<json.count;i++)
    {
        img=[NSString stringWithFormat:@"%@",[json objectAtIndex:i]];
        NSLog(@"Data is arrayimage%@",img);
    }


   NSURL *urlOne=[NSURL URLWithString:img];
    NSLog(@"Data is arrayurl%@",urlOne);
    NSData *newData=[NSData dataWithContentsOfURL:urlOne];

    UIImageView *imaegView=[[UIImageView alloc]initWithFrame:CGRectMake(38,0, 76, 96)];


    [imaegView setImage:[UIImage imageWithData:newData]];

    [self.myCollectionView addSubview:imaegView];


    [[self myCollectionView]reloadData];

}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return  1;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return  [arrayOfImages count];
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"Cell";
    CustomCell *cell=[ collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [[cell myImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
   // [[cell myLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
    return cell;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

i tried this code but in my output image are not display in my collectionView please tell me how to retrieve images from json to My-collection VIew

Upvotes: 0

Views: 1551

Answers (1)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

As your JSON contains Image.

It must be encoded while transmitting mostly in Base64. Now you need to decode it to form NSData.

Once you have converted it to NSData then you can easily form an UIImage from it, after this you can show the image on collection view.

Upvotes: 1

Related Questions