Коллекция Просмотр динамической высоты на основе изображения

Я использую представление коллекции, в котором ячейка содержит два изображения. Теперь я хочу, чтобы высота ячейки была динамической на основе высоты изображения. Вот код

   - (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];
            }];

        }];

но я получаю, что вся высота ячейки одинакова, и изображения в ней растягиваются. Вот скриншот одной ячейки

Изображение растянуто


person Muhammad Salman    schedule 30.06.2016    source источник


Ответы (1)


Чтобы исправить растягивание изображения, установите

_imageView.contentMode = UIViewContentModeScaleAspectFill;

Что касается размера ячейки представления коллекции, поставьте точки останова в cellForItemAtIndexPath и проверьте правильность или неправильность расчета.

person Proton    schedule 30.06.2016