Search code examples
objective-cuicollectionviewnsarray

Array of image url to ui collectionview


I am doing a json query to get an array of image urls. I want to display the images on a uicollection view. But I am missing something. I think I have to parse thru the array and set nsurl to each item. Then put each item in nsdata. But I am not sure. I don't want to use 3rd party software either.

Here is a sample of my code.

- (void)viewDidLoad {

    [super viewDidLoad];

    dispatch_async(kBgQueue, ^{
       NSData* data = [NSData dataWithContentsOfURL:
                    coProFeedURL];
       [self performSelectorOnMainThread:@selector(fetchedData:)
                           withObject:data waitUntilDone:YES];
   });
}

///new return all company data array
- (void)fetchedData:(NSData *)responseData {

   //parse out the json data
   NSError* error;
   NSDictionary* json = [NSJSONSerialization
                        JSONObjectWithData:responseData //1
                        options:kNilOptions
                        error:&error];

   NSArray * getProductsArray = [json objectForKey:@"CompanyProduct"]; //2  get all product info

   NSPredicate *predicate = [NSPredicate predicateWithFormat:@"companyID = %@", passCoData];//added create filter to only selected company


   NSArray * filteredProductArray = [getProductsArray filteredArrayUsingPredicate:predicate];//only products for selected company
  ///doing parsing here to get array of image urls

  finalImageArray = [filteredProductArray allObjects];//original
  NSLog(@" url  images :%@", finalImageArray);
  //NEED TO GET FINALIMAGEARRAY TO NSURL TYPE AND SET TO IMAGE?
}

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

  return finalImageArray.count; 
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *identifier = @"Cell";//test form file works
  ItemCollectionViewCell *cell = (ItemCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];//test form file works

  ///then add the finalImageArray?    
  return cell;
}

Here the output of my finalImageArray that i want to apply to uicollectionview, here is the log data: {( "http://www.test/inventory/images/bball.jpg", "http://www.test/images/bird%20tank_0.jpg" )}

So am i missing something like nsurl, or nsdata, or what. How do i get this array of image url to display on a uicollectionview? Thanks for the help!


Solution

  • Just reload Collection view with updated data of array. As you requested on background thread. Now, you need to reload data on main thread.

    - (void)fetchedData:(NSData *)responseData {
    
      //YOUR CODE ...
    
      finalImageArray = [filteredProductArray allObjects];//original
      NSLog(@" url  images :%@", finalImageArray);
      //NEED TO GET FINALIMAGEARRAY TO NSURL TYPE AND SET TO IMAGE?
    
      dispatch_async(dispatch_get_main_queue(), ^{
    
         [collectionView reloadData];
      });
    }
    

    Hope, it'll help you.