title: I'm trying to download a list of photos from URL displayed in collectionview, but I cannot figure out a way to do this
body: <p>I need to get several URLs of photos from a server and display them in a collectionview. The thing is, they provide a api called 
  "findObjectsInBackgroundWithBlock" to let me download URLs from server. It automatically download from server in background.I'm trying to use dispatch_async but it seems has some trouble with collectionview. Any ideas?</p>
  
  <pre><code> BmobQuery *picturesB = [BmobQuery queryWithClassName:@"AssetPicture"];
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  [picturesB findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error)
   {
       for (BmobObject *obj in array)
       {
           [photourl addObject:[obj objectForKey:@"imageUrl"]];
       }
       for (int i = 0; i &lt; 12; i++) {
           NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:photourl[i]]];
           [self.arr addObject:[UIImage imageWithData:imageData]];  
       }     
   }];//Bmob query        
  
  
  if(self.arr!=nil)
     {
      dispatch_async(dispatch_get_main_queue(), ^{         
      UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
      [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
      self.collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
      [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionCell"]; 
      self.collectionView.backgroundColor = [UIColor whiteColor];
      [self.view addSubview:self.collectionView];   
      self.collectionView.dataSource = self;
      self.collectionView.delegate = self;
      [self.collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
         });// dispatch
     }//if
    });//dispatch
  
  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  
  CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionCell" forIndexPath:indexPath];
  cell.imageView.image = [self.arr objectAtIndex:indexPath.row];
  NSLog(@"photo done");
  cell.descLabel.text = [[NSString alloc] initWithFormat:@"{%ld,%ld}",indexPath.section,indexPath.row];
  return cell;
  </code></pre>
  
  <p>}</p>