-(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,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;
}
但是,集合视图中显示纵向和横向图像.肖像图像显示得很好,但在集合视图中景观图像看起来很小.我需要做的是根据图像大小调整单元格大小,以便填充正确的宽高比?
我添加了如下所示的方法,但问题是它在开始时一次调用所有单元格,并且一旦我在cellForRowAtIndexPath方法中下载图像就不会被调用:
- (CGSize)collectionVIEw:(UICollectionVIEw *)collectionVIEw layout:(UICollectionVIEwLayout*)collectionVIEwLayout sizeforItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(165,255);
}
因此,我想知道如何实现此功能,如果需要更多信息,请告诉我们?
解决方法 在-setimageWithURLRequest:…中,当图像加载将其缓存在哈希表中,然后使用当前图像路径调用-reloadItemsAtIndexPaths:重新加载单元格,从而使CollectionVIEw重新检查单元格的大小应该是多少.确保在查找图像时查阅哈希表.如果collectionVIEw项目在屏幕外滚动并重新打开,这还可以节省额外的网络访问.
注意我担心如果您使用UICollectionReusableVIEw子类,您的代码可能会失败,因为单元格可以在其图像加载之前重新使用,因此图像将加载到错误的位置.
总结以上是内存溢出为你收集整理的ios – 根据UICollectionView中的图像动态更改单元格大小全部内容,希望文章能够帮你解决ios – 根据UICollectionView中的图像动态更改单元格大小所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)