IOS: UICollectionView 未在单元格中显示完整图像

IOS: UICollectionView not showing full images in cells

我在一个使用 UICollectionView 显示图像的项目中工作。我应用了所有委托方法并且它工作正常但是 UICollectionView Cell 中显示的图像未显示全尺寸仅显示部分 请建议我必须在代码中做哪些更改才能在 UICollectionViewCell

中显示完整图像

我应用了以下代码:

.h 文件

#import <UIKit/UIKit.h>

@interface photos : UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *_collectionView;
    NSMutableArray * imagearray;
    UIImageView *imgview;
}


@end

.m 文件

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    _collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [_collectionView setBackgroundColor:[UIColor whiteColor]];


    [self.view addSubview:_collectionView];

    imagearray = [[NSMutableArray alloc]initWithObjects:@"download3.jpeg", @"12045540_717548405011935_7183980263974928829_o.jpg" , @"download4.jpeg", @"download5.jpeg", @"download6.jpg", @"download12.jpeg", @"download13.jpeg", @"download16.jpeg",@"download3.jpeg", @"download6.jpg", @"download12.jpeg", @"download16.jpeg", @"download3.jpeg", @"12045540_717548405011935_7183980263974928829_o.jpg" , @"download4.jpeg", @"download5.jpeg", @"download6.jpg", @"download12.jpeg", nil];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [imagearray count];
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    imgview = (UIImageView *)[cell viewWithTag:100];
    imgview.image = [UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]];

    imgview.contentMode = UIViewContentModeScaleAspectFit;
    imgview.clipsToBounds = YES;
    //cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
    [self.view addSubview:imgview];
    cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]]];


    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(100, 100);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(50, 20, 50, 20);
}
  1. 好的,所以首先要确保你的图像视图的框架 是正确的。这是我对另一个问题的回答,但想法是 调试视图层次结构也适用于您的情况(请参阅 那里的截图):.

  2. 如果是因为帧错误,就用:

    imgView.frame = CGRect(0.0, 0.0, cell.contentView.bounds.size.width/4, cell.contentView.bounds.size.height)

(它将图像视图的框架设置为位于单元格的左上角并占据四分之一宽度)。

cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]]];

将重复 imageView 而您看不到正确的图像。 如果您想在集合视图单元格中显示图像视图,

  1. 将 UIImageView 添加到您的 collectionViewCell

  2. 您可以在 CellForItem ->

    中设置此 UIImageView 的图像
    cell.imageview.image =[UIImage imageNamed :@"image name"];
    cell.imageview.contentMode = UIViewContentModeScaleAspectFit;
    

如果我不明白这个问题,请解释更多。

我没有初始化我的 UIImageView 这就是为什么会出现这个问题

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

    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    imgview = (UIImageView *)[cell viewWithTag:100];
    imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,160, 160)];

    imgview.image = [UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]];

    imgview.contentMode = UIViewContentModeScaleAspectFit;
    //imgview.clipsToBounds = YES;

    cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]]];
    [cell.contentView addSubview:imgview];

    return cell;
}