我正在使用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);
}
UIViewContentModeScaleAspectFit要细,我没有初始化我
UIImageView
多数民众赞成。在图像视图上看起来像一个错误的框架。 –我已经使用它,但结果相同 –
@ArthurGevorkyan我如何为imageview设置框架? –