0
私は、セル上に2つの画像が含まれているコレクションビューを使用しています。今、私はそのセルの高さが画像の高さに基づいて動的であることを望みます。ここでは、コードコレクション画像に基づいて動的な高さを表示
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// CGSize defaultSize = [(UICollectionViewFlowLayout*)collectionViewLayout itemSize];
if (indexPath.section==[dataArray count]) {
return CGSizeMake(screenWidth,50);;
}else{
PMNewsFeedObject *feedObj=[dataArray objectAtIndex:indexPath.section];
if (feedObj.captionPopulateMore) {
float height=[self findHeightForText:feedObj.caption havingWidth:screenWidth-30 andFont:[UIFont fontWithName:@"MSReferenceSansSerif" size:15]];
height=height-10;
return CGSizeMake(feedObj.imageSize.width, feedObj.imageSize.height+height);
}else{
return CGSizeMake(feedObj.imageSize.width, feedObj.imageSize.height+45);
}
}
return CGSizeMake(0,0);
であり、ここでcellforitematindexpath
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section==dataArray.count) {
UICollectionViewCell *loadmoreCell=[homeCollectionView dequeueReusableCellWithReuseIdentifier:@"loadmoreCell" forIndexPath:indexPath];
UIActivityIndicatorView *loadmoreIndicator=(UIActivityIndicatorView*)[loadmoreCell viewWithTag:100];
[loadmoreIndicator startAnimating];
return loadmoreCell;
}
PMNewsFeedObject *feedObj=[dataArray objectAtIndex:indexPath.section];
PMHomeCollectionViewCell *cell=[homeCollectionView dequeueReusableCellWithReuseIdentifier:@"homeCell" forIndexPath:indexPath];
NSString *imageURLString=feedObj.petImage;
NSString *image2URLString = feedObj.petImage2;
if (imageURLString && ![imageURLString isEqual:[NSNull null]])
{
NSURL *imageURL = [NSURL URLWithString:[[NSString stringWithFormat:@"%@%@",BASE_URL,imageURLString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[cell.petImgView sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"placeholder_pet.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[UIView animateWithDuration:0.0f animations:^{
UIImage *newImg=[image resizedImageByWidth:flowLayout.itemSize.width];
PMNewsFeedObject *newObject=[dataArray objectAtIndex:indexPath.section];
NSLog(@"%f",flowLayout.itemSize.width);
NSLog(@"%f",flowLayout.itemSize.height);
// float hfactor = image.size.width/flowLayout.itemSize.width;
// float vfactor = image.size.height /flowLayout.itemSize.height;
//
// NSLog(@"%f",flowLayout.itemSize.width);
// NSLog(@"%f",flowLayout.itemSize.height);
//
// float newHeight=0;
// if (image.size.width<image.size.height) {
//
// float factor = fmax(hfactor, vfactor);
// newHeight= image.size.height/factor;
// }else{
//
//
// float factor = fminf(hfactor, vfactor);
// newHeight= image.size.height * factor;
//
//
// }
// NSLog(@"%f",newImg.size.height);
if (newImg.size.height!=0) {
newObject.imageSize = newImg.size;
NSLog(@"%f",newImg.size.width);
NSLog(@"%f",newImg.size.height);
}
[dataArray replaceObjectAtIndex:indexPath.section withObject:newObject];
[self.homeCollectionView.collectionViewLayout invalidateLayout];
}];
}];
ためのコードですが、私はすべてのセルの高さが同じであるとイメージがそれでストレッチしている得ます。ここで は、コレクションビューセルのサイズについて
_imageView.contentMode = UIViewContentModeScaleAspectFill;
を設定し、ストレッチ画像を修正するための一つのセルのスクリーンショット
私の質問を編集してくださいもう一度確認してください – salmancs43
OK、回答を更新しました – Proton