IOS: UICollectionView not showing full images in cells

时间:2015-10-06 08:32:39

标签: ios uicollectionview uicollectionviewcell

i am working in a project in which i am using UICollectionView for displaying images. i applied all the delegate methods and it works fine but the images shows in UICollectionView Cell is not shown full size only some part is shown please suggest me what changes i have to do in my code to show full images in UICollectionViewCell

I applied the following Codes:

.h file

#import <UIKit/UIKit.h>

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


@end

.m file

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

3 个答案:

答案 0 :(得分:2)

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

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

  1. 将UIImageView添加到collectionViewCell

  2. 你可以在CellForItem上设置这个UIImageView的图像 - &gt;

    cell.imageview.image =[UIImage imageNamed :@"image name"];
    cell.imageview.contentMode = UIViewContentModeScaleAspectFit;
    
  3. 如果我不理解plz解释的问题。

答案 1 :(得分:1)

我没有初始化我的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;
}

答案 2 :(得分:0)

  1. Alright, so first of all make sure that the frame of your image view is correct. Here is my answer to another question but the idea of debugging the view hierarchy applies to your case as well (see screenshots there): https://stackoverflow.com/a/32861703/2799410.

  2. If it's due to a wrong frame, just use:

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

(it sets the frame of your image view to be located at the top left corner of your cell and take a quarter width).