gpt4 book ai didi

ios - 根据 UICollectionView 中的图像动态更改单元格大小

转载 作者:可可西里 更新时间:2023-11-01 03:06:55 24 4
gpt4 key购买 nike

我在水平 Collection View 中显示从服务器收到的动态图像。当我设置 collectionview 时,我设置了这个:

-(void)setupCollectionView{
[self setupPageControlView];
self.screensCollectionView.delegate=self;
self.screensCollectionView.dataSource=self;
[self.screensCollectionView registerNib:[UINib nibWithNibName:@"FGAppScreensCollectionViewItemCell" bundle:nil] forCellWithReuseIdentifier:@"screenCell"];


UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[flowLayout setItemSize:CGSizeMake(165, 255)];
//[self.screensCollectionView setPagingEnabled:YES];
[self.screensCollectionView setCollectionViewLayout:flowLayout];

对于 cellForRowAtIndexPath,我这样下载图像:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"screenCell";

FGAppScreensCollectionViewItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[self.imageURLArray objectAtIndex:indexPath.row]]];

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
[spinner setFrame:CGRectMake(50, 100, 20, 20)];
[cell.contentView addSubview:spinner];
[cell.screenImageView setImageWithURLRequest:urlRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
//STOP THE SPINNER
for (UIView* view in [cell.contentView subviews]) {
if ([view isKindOfClass:[UIActivityIndicatorView class]]) {
[view removeFromSuperview];
}
}
cell.screenImageView.image = image;

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
//
}];
[cell.screenImageView setImageWithURL:[NSURL URLWithString:[self.imageURLArray objectAtIndex:indexPath.row]]];
if (self.delegate) {
UITapGestureRecognizer* g = [[UITapGestureRecognizer alloc] initWithTarget:self.delegate action:@selector(showPopUpImageView:)];
[cell.screenImageView addGestureRecognizer:g];
}

return cell;

但是, Collection View 中显示纵向和横向图像。纵向图像显示得很好,但横向图像在 Collection View 中显得很小。 我需要做的是根据图像大小调整单元格的大小,以便它们填充正确的纵横比

我在下面添加了这样的方法,但问题是它在开始时会同时调用所有单元格,一旦我在 cellForRowAtIndexPath 方法中下载图像,它就不会被调用:

    - (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

return CGSizeMake(165, 255);

因此,我想知道如何实现此功能,如果需要这方面的更多信息,请告诉我?

最佳答案

-setImageWithURLRequest:...中,当图片加载时缓存在哈希表中,然后调用-reloadItemsAtIndexPaths:当前图片路径,重新加载单元格,从而导致 CollectionView 重新检查单元格的大小。

确保在查找图像时引用哈希表。如果 collectionView 项目滚动到屏幕外并再次打开,这也将为您节省额外的网络访问。

请注意,如果您使用 UICollectionReusableView 子类,我担心您的代码可能会失败,因为单元格可以在图像加载之前重新使用,因此图像将加载到错误的位置.

关于ios - 根据 UICollectionView 中的图像动态更改单元格大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21525034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com