我有一个名为UICollectionViewCell
的{{1}}子类,其中包含一个名为AlbumCVC
的{{1}} --- 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;
}
的NSMutableArray。我确定该属性设置正确,因为我在记录albumPhotos
时得到了明智的结果。当我设置背景颜色时,单元格的大小也正确,因为框架是可见的。但由于某种原因,ALAsset
将无法显示。我需要在albumCVC.cellImage.image.bounds.size
内进行另一个方法调用才能显示图像吗?
更新:根据一个非常聪明的朋友的建议,我尝试将cellImage
移出单元格,将其放在主视图的其他位置,一切都很可爱。问题似乎与collectionView:cellForItemAtIndexPath:
的框架/边界有关。我认为我需要进行一个方法调用,以便在调用UIImageView
之后,单元格的子视图扩展为适合新调整大小的单元格。现在的问题是UIImageView
是一个只读属性,所以我无法直接调整它。
更新2:在另一条建议中,我查看了单元格collectionView:layout:sizeForItemAtIndexPath:
和UIImageView.image.size
的边框和边界,发现它们没有匹配。添加了另一个方法调用以使它们相等,甚至将contentView
更改为cellImage
以尝试让单元格正确呈现缩略图。不幸的是,我仍然在巨大的细胞内获得微小的缩略图。知道为什么吗?上面和下面的更新代码。
为了完整起见,这是整个类的实现:
contentMode
更新3:我无法确定最初的问题是什么,但我知道它现在适用于以下UIViewContentModeScaleAspectFit
实施:
#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
任何地方都没有框架或边界,一切正常。也许这是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;
}
之间的区别?
答案 0 :(得分:1)
我遇到了类似的问题,并通过将UICollectionViewCell框架属性设置为与UIImageView的框架相同来解决它。我不是100%确定这是你的问题,我只是在代码中构建集合(没有故事板)