2015-10-06 46 views
1

我正在使用UICollectionView显示图像的项目中工作。我应用了所有委托方法,它工作正常,但图像显示在UICollectionView细胞是未显示全尺寸只有一些部分显示 请建议我什么样的变化,我有我的代码做显示UICollectionViewCelliOS:UICollectionView不显示单元格中的完整图像

完整图像我使用了以下代码:

.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); 
} 
+0

UIViewContentModeScaleAspectFit要细,我没有初始化我UIImageView多数民众赞成。在图像视图上看起来像一个错误的框架。 –

+0

我已经使用它,但结果相同 –

+0

@ArthurGevorkyan我如何为imageview设置框架? –

回答

2
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; 
    

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

+0

我使用自定义集合视图为我的应用程序它被添加和工作正常,但是当我应用的图像数组显示在单元格上它只显示图像的一小部分,如果我使用500 * 500分辨率的图像,它只显示图像顶部的160 * 160部分,我在魔杖中显示完整的图像。我只应用上面的完整代码没有故事板,所以请查看代码并回答它 –

+0

我的意思是不要使用单元格背景来分配图像,在您的自定义单元格中使用相同的单元格框将init初始化为UIImageView并将该图像设置为此视图在单元格为 – engmahsa

+0

当你使用“colorWithPatternImage”,你不能看到正确的图像大小 – engmahsa

0
  1. 好吧,首先确保图像视图的框架 是正确的。这是我对另一个问题的回答,但调试视图层次结构 也适用于您的案例(请参阅 屏幕截图):https://stackoverflow.com/a/32861703/2799410

  2. 如果它是由于一个错误的帧,只使用:

    imgView.frame =的CGRect(0.0,0.0,cell.contentView.bounds.size.width/4,cell.contentView.bounds.size。高度)

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

+0

不工作 –

+0

可能有很多原因:布局限制不正确,单元格本身尺寸错误等。我们最好聊聊天。访问http://chat.stackoverflow.com/users/2799410/arthur-gevorkyan并开始对话。 –

+0

我不使用故事板所有的代码是自定义的 –

1

为什么会出现这个问题

- (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; 
} 
相关问题