一系列图像网址到ui collectionview

时间:2016-02-11 20:11:48

标签: objective-c uicollectionview nsarray

我正在进行json查询以获取图像网址数组。我想在一个uicollection视图上显示图像。但我错过了一些东西。我想我必须解析数组并将nsurl设置为每个项目。然后将每个项目放在nsdata中。但我不确定。我也不想使用第三方软件。

以下是我的代码示例。

- (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;
}

这里是我想要应用于uicollectionview的finalImageArray的输出,这里是日志数据:      {(         " http://www.test/inventory/images/bball.jpg&#34 ;,         " http://www.test/images/bird%20tank_0.jpg"      )}

所以我错过了像nsurl,nsdata或者什么的东西。如何在uicollectionview上显示这个图像URL数组?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

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.