Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从web加载图像并在collectionview中显示_Ios_Uicollectionview_Sdwebimage - Fatal编程技术网

Ios 从web加载图像并在collectionview中显示

Ios 从web加载图像并在collectionview中显示,ios,uicollectionview,sdwebimage,Ios,Uicollectionview,Sdwebimage,我正在使用UICollectionView显示一组图像。这些图像是从web加载的。我从在线JSON文件中获取图像URL。图像大小不同,我使用flowlayout将图像放在一起。这里的挑战是,在加载图像之前,您无法知道图像的确切大小。我使用SDWebImage下载和缓存图像。我使用一个布尔值来检查加载了哪些图像。加载每个图像后,我告诉collectionview在其相应的indexpath处重新加载图像。现在的问题是,图像确实加载正确,但当我滚动collectionview时,单元格都被弄乱了。下

我正在使用UICollectionView显示一组图像。这些图像是从web加载的。我从在线JSON文件中获取图像URL。图像大小不同,我使用flowlayout将图像放在一起。这里的挑战是,在加载图像之前,您无法知道图像的确切大小。我使用SDWebImage下载和缓存图像。我使用一个布尔值来检查加载了哪些图像。加载每个图像后,我告诉collectionview在其相应的indexpath处重新加载图像。现在的问题是,图像确实加载正确,但当我滚动collectionview时,单元格都被弄乱了。下面是一些代码:

这是我计算每个单元格大小的地方:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(NHBalancedFlowLayout *)collectionViewLayout preferredSizeForItemAtIndexPath:(NSIndexPath *)indexPath
   {

if (didLoadImage == YES) {
   return [loadedImage size];
   }
else
   {
    return CGSizeMake(300, 140);
   }
}
这里是加载和设置图像的位置:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
GalleryCell *customCell = [collectionView
                           dequeueReusableCellWithReuseIdentifier:@"cell"
                           forIndexPath:indexPath];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[galleryLargeLinks objectAtIndex:indexPath.row]]];

customCell.backgroundColor = [UIColor blackColor];

[customCell.imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"placeholder"] success:^(NSURLRequest *request, NSHTTPURLResponse *response,UIImage *image)
 {
     loadedImage = image;
     didLoadImage = YES;
     [customCell.imageView setImage:image];
     [collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];

 } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
 {
     NSLog(@"fail");
     [activityIndicator stopAnimating];
 }];
return customCell;
}

UICollectionViewCell的
在滚动到屏幕外时将被重用。因此,由于您使用的是异步块来加载它们,所以当单元格准备就绪时,它可能会被分配给另一个indexPath。如果愿意,您可以继续使用
setImageWithURLRequest:
,但是,您应该添加另一个检查,以查看在加载indexPath时,它是否仍然是您所期望的。大概是这样的:

[customCell setIndexPath:indexPath];
__weak GalleryCell *weakCell = cell;
__weak CollectionView *weakCollectionView = collectionView;
[customCell.imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"placeholder"] success:^(NSURLRequest *request, NSHTTPURLResponse *response,UIImage *image)
 {
     if([weakCell.indexpath isEqual:indexPath]){
         [weakCell.imageView setImage:image];
         [weakCollectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
     }

 } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
 {
     [activityIndicator stopAnimating];
 }];

至于
collectionView:layout:preferredSizeForItemAtIndexPath:
,您需要以完全不同的方式进行检查。一种选择是将缓存的图像从数据源中删除,这样您就可以检查数据源数组中的值并查看它是否在缓存中。如果是,则抓取图像,否则返回默认值。这太多的代码无法写出,但基本上您应该检查数据源/缓存,而不仅仅是两个可以随时更改的随机
BOOL
UIImage
变量。

感谢您的回复。但是我如何比较weakcell.indepath和indepath呢?没有setindexpath方法。您需要在CustomCell类->NSIndexPath*indexPath;