0

我有一个UICollectionViewCell名为的子类AlbumCVC,其中包含一个IBOutlet--- 一个UIImageView被调用的cellView. 我cellView在以下方法中为每个单元格设置值:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell;

    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"albumPhotoCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blueColor];
    if ([cell isKindOfClass:[AlbumCVC class]]){
        AlbumCVC *albumCVC = (AlbumCVC *)cell;
        ALAsset *thisImage = [self.albumPhotos objectAtIndex:indexPath.item];        

        albumCVC.imageView.frame = albumCVC.contentView.frame;
        albumCVC.contentView.contentMode = UIViewContentModeScaleAspectFit;
        albumCVC.imageView.image = [UIImage imageWithCGImage:[thisImage aspectRatioThumbnail]];

        }
    }
    return cell;
}

其中albumPhotosALAssets 的 NSMutableArray。我确信该属性设置正确,因为我在登录albumCVC.cellImage.image.bounds.size. 当我设置背景颜色时,单元格的大小也正确,因为框架是可见的。但由于某种原因,cellImage不会显示。collectionView:cellForItemAtIndexPath:为了显示图像,我需要在内部进行其他方法调用吗?

更新:在一位非常聪明的朋友的建议下,我尝试将其UIImageView移出单元格,将其放在主视图的其他位置,一切都很顺利。该问题似乎与UIImageView. 我认为我需要进行一个方法调用,以便单元格的子视图在调用collectionView:layout:sizeForItemAtIndexPath:. 现在的问题是这UIImageView.image.size是一个只读属性,所以我不能直接调整它的大小。

更新 2:在另一条建议中,我查看了单元格的框架和边界,contentView发现cellImage它们不匹配。添加了另一个方法调用以使它们相等,甚至更改contentModeUIViewContentModeScaleAspectFit以尝试使单元格正确呈现缩略图。不幸的是,我仍然在巨大的单元格中看到微小的缩略图。知道为什么吗?更新了上面和下面的代码。

为了完整起见,这是整个类的实现:

#import "AlbumViewController.h"
#import "AlbumCVC.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface AlbumViewController ()
@end

@implementation AlbumViewController


#pragma constants

const int IPHONE_WIDTH = 320;

#pragma delegate methods

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section{

    // Get the count of photos in album at index albumIndex in the PhotoHandler
    NSInteger numCells = [self.group numberOfAssets];
    return numCells;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell;

    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"albumPhotoCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blueColor];
    if ([cell isKindOfClass:[AlbumCVC class]]){
        AlbumCVC *albumCVC = (AlbumCVC *)cell;
        ALAsset *thisImage = [self.albumPhotos objectAtIndex:indexPath.item];
        }

        albumCVC.imageView.frame = albumCVC.contentView.frame;
        albumCVC.contentView.contentMode = UIViewContentModeScaleAspectFit;
        albumCVC.imageView.image = [UIImage imageWithCGImage:[thisImage aspectRatioThumbnail]];

    }
    return cell;
}

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


    //Copy a pointer to an asset from the album
    ALAsset *thisImage = [self.albumPhotos objectAtIndex:indexPath.item]; //Zen - are you sure thisImage represents a valid image?

    //Copy that asset's size and create a new size struct
    CGSize thisSize = thisImage.defaultRepresentation.dimensions;
    CGSize returnSize;

    // force all previews to be full width
    returnSize.width = IPHONE_WIDTH;
    returnSize.height = IPHONE_WIDTH * thisSize.height / thisSize.width;

    return returnSize;
}


#pragma lifecycle methods

- (void)viewWillAppear:(BOOL)animated{
    [self.albumPhotos removeAllObjects];

//"handler" is a class that manages calls to the ALAssetLibrary. self.albumIndex is an integer that gets set on segue. As far as I can tell, everything in the below method is working fine --- cells are sized properly.

    self.group = self.albumDelegate.handler.groups[self.albumIndex];
    [self.group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if (result) {
            NSLog(@"Just added an object to albumPhotos.");
            [self.albumPhotos addObject:result];
            NSLog(@"The item in albumPhotos is class %@", [self.albumPhotos[0] class]);
        }
    }]; 

}


#pragma instantiation

- (ALAssetsGroup *)group{
    if (!_group) {
        _group = [[ALAssetsGroup alloc]init];
    }
    return _group;
}

- (NSMutableArray *)albumPhotos{
    if (!_albumPhotos) {
        _albumPhotos = [[NSMutableArray alloc]init];
    }
    return _albumPhotos;
}

@end

更新 3:我无法确定最初的问题是什么,但我知道它现在适用于以下cellForItem实现:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell;
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"albumPhotoCell" forIndexPath:indexPath];
    if ([cell isKindOfClass:[AlbumCVC class]]) {
        AlbumCVC *albumCVC = (AlbumCVC *)cell;
        albumCVC.albumImageView.image = [[UIImage alloc] initWithCGImage:[self.albumAssets[[self reverseAlbumIndexForIndex:indexPath.item]] thumbnail]];
    }
    cell.alpha = [self alphaForSelected:self.selectedItems[@(indexPath.item)]];
    return cell;
}

任何地方都没有框架或边界,一切正常。[[UIImage alloc]initWithCGImage]也许这是和之间的区别[UIImage imageWithCGImage]

4

1 回答 1

1

我有一个类似的问题,并通过将 UICollectionViewCell 框架属性设置为与 UIImageView 的框架相同来解决它。我不是 100% 确定这是你的问题,我纯粹是在代码中构建集合(没有故事板)

于 2013-07-22T23:20:38.073 回答