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()]
})
}
现在Image来自Json和Store in imageArray和JSON但是当我向下滚动集合视图时,请调用此方法func scrollViewDidEndDecelerating(scrollView: UIScrollView)
将应用程序冻结2-3分钟。请给我一些解决方案。
并且先谢谢你。
答案 0 :(得分:0)
您可以使用CHTCollectionViewWaterfallLayout
https://github.com/chiahsien/CHTCollectionViewWaterfallLayout
这就是我所做的
- (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];
}
希望这会有所帮助。
答案 1 :(得分:0)
可以在mainThread中下载图像。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// do some task
dispatch_async(dispatch_get_main_queue(), ^{
// update some UI
});
});