在UICollectionViewCell中圈出ImageView

时间:2015-12-29 06:44:03

标签: ios objective-c uicollectionview

2015年12月29日更新 所以这显然适用于UICollectionViewCell类:

- (void)setThumbnailImage:(NSString *)thumbnailImage {
   _thumbnailImage = [thumbnailImage copy];


   self.thumbnailImageView.image = [UIImage imageNamed:thumbnailImage];
   if (self.thumbnailImageView.layer.frame.size.width > 82.0f) {
    self.thumbnailImageView.layer.cornerRadius = self.thumbnailImageView.frame.size.width/6;
   }
   else {
    self.thumbnailImageView.layer.cornerRadius = self.thumbnailImageView.frame.size.width/2;
   }
   self.thumbnailImageView.clipsToBounds = YES;
   self.thumbnailImageView.layer.borderColor = [UIColor whiteColor].CGColor;
   self.thumbnailImageView.layer.borderWidth = 4.0f;
}

我不知道为什么ImageView框架在刷新后而不是第一次加载时更新。有人可以详细说明吗?

问题

我正在努力尝试获取ImageView的正确帧大小,以便我可以将其设为圆形而不是方形。

UICollectionViewController中,我在- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath代码中创建了一个ImageView圈cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width/2;

问题是ImageView是菱形的,因为帧大小不正确但是当我向上和向下滚动时,刷新的单元格是圆形的,并且具有正确的框架大小,它应该在故事板中进行。< / p>

Here is the refreshed CollectionView

以下是UICollectionViewController

中代码的摘要
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setupDKScrollingView];

names = @[@"Joe Smith", @"Veronica Odel", @"Lucas Baze", @"Brian Can", @"Cory Cobler", @"Emily Jenson"];
timeRemaining = @[@"Overdue", @"0 days", @"Past", @"1 month", @"2 days", @"4 days"];
thumbnails = @[@"gray_1",@"gray_1",@"gray_1",@"gray_1",@"gray_1",@"gray_1"];

[self.collectionView reloadData]; 
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return [names count];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
 }

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

RZEContactsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ContactCell" forIndexPath:indexPath];

//cell.nameLabel.text = names[indexPath.row];
//cell.timeRemainingLabel.text = timeRemaining[indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:thumbnails[image.row]];
cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width/2;
cell.thumbnailImageView.layer.borderWidth = 2.0f;
cell.thumbnailImageView.layer.borderColor = [UIColor blackColor].CGColor;
cell.layer.masksToBounds = YES;

cell.backgroundColor = [UIColor blueColor];
cell.layer.cornerRadius = 5.0f;

return cell;
}

2 个答案:

答案 0 :(得分:1)

试试这个

static NSString *identifier = @"Cell";

CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

cell.collectionImgView.clipsToBounds=YES;
cell.collectionImgView.layer.cornerRadius=cell.collectionImgView.frame.size.width/2;
cell.collectionImgView.image = [UIImage imageNamed:[_photosArr objectAtIndex:indexPath.row]];

return cell;

答案 1 :(得分:0)

您可能无法在每台设备上获得完美的圆圈,请尝试将 2.0 更改为不同尺寸设备的不同值。

{   
cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width/2.0;
cell.thumbnailImageView.clipsToBounds = YES;
}