pradip kikani pradip kikani - 4 years ago 1089
Swift Question

Dynamically change the collectionview cell size

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()]

})

}


Now the Image comes from Json and Store in imageArray and JSON but when I Scroll down collection view then call this method
func scrollViewDidEndDecelerating(scrollView: UIScrollView)
Afer the Application Freeze For 2-3 Minutes. Please give me some solution.
And Thank you In Advance.

Answer Source

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

    });
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download