func collectionView(collectionView: UICollectionView,  cellForItemAtIndexPath indexPath: NSIndexPath) ->  UICollectionViewCell
 {
         MBProgressHUD.hideHUDForView(self.view, animated: true)

        var url : String
              let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! celll
            let imagess : UIImageView = cell.image

          url = self.imageArray.objectAtIndex(indexPath.row).valueForKey("photo-url-500") as! String
         [imagess.sd_setImageWithURL(NSURL(string: url), placeholderImage: nil)]


          return cell


     }

   func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize
       {
                let cgsize : CGSize = CGSizeMake(CGFloat(A_size_width.objectAtIndex(indexPath.row) as! NSNumber), CGFloat(A_size_height.objectAtIndex(indexPath.row) as! NSNumber))
               return cgsize
           }


 func scrollViewDidEndDecelerating(scrollView: UIScrollView)
 {
  let bottomFloat : CGFloat = scrollView.contentOffset.y + scrollView.frame.height
  if bottomFloat >= scrollView.contentSize.height
  {
     let str   = (json.valueForKey("posts-total") as! NSNumber)
     let value:Int  = Int(str)

     if (StartNumber < value)
     {
        reachedEnd = false;
        self.jsonParsingFromURL(StartNumber)
        StartNumber = StartNumber + 30;

     }
     else
     {
        reachedEnd = true;
        [self.IMG_collection .reloadSections(NSIndexSet(index: 0))]
     }

  }

}





 func fileheight_width()
{
  let qualityOfServiceClass = QOS_CLASS_BACKGROUND
  let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
  dispatch_async(backgroundQueue, {

     dispatch_async(dispatch_get_main_queue(), { () -> Void in

        if self.StartNumber == self.imageArray.count
        {
        for var i = 0 ; i < self.imageArray.count ; i++
        {
           let str :String = self.imageArray.objectAtIndex(i).valueForKey("photo-url-500") as! String
           let Imagesource : CGImageSourceRef = CGImageSourceCreateWithURL(NSURL(string: str)!, nil)!
           let pixelHeight = kCGImagePropertyPixelHeight as String
           let pixelWidth = kCGImagePropertyPixelWidth as String
           let imgProperty = CGImageSourceCopyPropertiesAtIndex(Imagesource, 0, nil) as NSDictionary!
           let height = imgProperty[pixelHeight] as! CGFloat!
           let width = imgProperty[pixelWidth] as! CGFloat!
           self.A_size_width.addObject(width)
           self.A_size_height.addObject(height)

        }
     }
        else
        {
           for var i = 0 ; i < self.imageArray.count ; i++
           {
              let str :String = self.imageArray.objectAtIndex(i).valueForKey("photo-url-500") as! String
              let Imagesource : CGImageSourceRef = CGImageSourceCreateWithURL(NSURL(string: str)!, nil)!
              let pixelHeight = kCGImagePropertyPixelHeight as String
              let pixelWidth = kCGImagePropertyPixelWidth as String
              let imgProperty = CGImageSourceCopyPropertiesAtIndex(Imagesource, 0, nil) as NSDictionary!
              let height = imgProperty[pixelHeight] as! CGFloat!
              let width = imgProperty[pixelWidth] as! CGFloat!
              self.A_size_width.addObject(width)
              self.A_size_height.addObject(height)

           }

        }
     })
     [self.IMG_collection.reloadData()]

  })

    }

Сейчас изображение приходит из Json и сохраняется в imageArray и JSON, но когда я прокручиваю представление коллекции вниз, то вызываю этот метод func scrollViewDidEndDecelerating(scrollView: UIScrollView) После этого приложение замирает на 2-3 минуты. Пожалуйста, дайте мне какое-нибудь решение. Заранее благодарю.

Ответы (2)

may b Images are download in mainThread .

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // do some task

dispatch_async(dispatch_get_main_queue(), ^{
        // update some UI

    });
});

You can use CHTCollectionViewWaterfallLayout

https://github.com/chiahsien/CHTCollectionViewWaterfallLayout.

Here is what i have done

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CatCollectionViewCell *catcollectionCell = [self.collectionCat dequeueReusableCellWithReuseIdentifier:@"catcell" forIndexPath:indexPath];
    if (catcollectionCell == nil) {
        [self.collectionCat registerNib:[UINib nibWithNibName:@"CatCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"catcell"];
    }
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[[[self.arrayData valueForKey:@"get_post_details"] valueForKey:@"post_photo"] objectAtIndex:indexPath.row]]];
    [catcollectionCell.imageCat setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        catcollectionCell.imageCat.image = image;
        if ([self.arraySize objectAtIndex:indexPath.row] == [NSNull null]){

            [self.arraySize insertObject:[NSValue valueWithCGSize:CGSizeMake(image.size.width, image.size.height)] atIndex:indexPath.row ];
            [self.collectionCat reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]];
       }
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

    }];
//    [catcollectionCell.imageCat setImageWithURL:[NSURL URLWithString:[[[self.arrayData valueForKey:@"get_post_details"] valueForKey:@"post_photo"] objectAtIndex:indexPath.row]]];
    return catcollectionCell;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        if ([self.arraySize objectAtIndex:indexPath.row] != [NSNull null] ){
            CGSize size = [[self.arraySize objectAtIndex:indexPath.row] CGSizeValue];
            NSInteger imageHeight = (size.height /size.width)*(self.view.frame.size.width - 20 ) /2;
            return CGSizeMake((self.view.frame.size.width - 20 ) /2 , imageHeight);
        }
        return CGSizeMake(150, 200);

//    return [self.arraySize[indexPath.item % 4] CGSizeValue];
}

Hope This will help .

2022 WebDevInsider