objective-c – UIImageView在UICollectionViewCell子类中显示超出范围的图像

我必须创建一个自定义UICollectionViewCell来显示图像的图像和名称.在图像的顶部,有一个帧图像,其宽度和高度比图像大2个像素.但是,无论我做什么,图像似乎都比帧图像大.我为表格视图做了同样的事情,它完美无缺.

这是代码

//GridCell.h

@interface GridCell : UICollectionViewCell
@property(nonatomic,strong) UILabel *lblName;
@property(nonatomic,strong) UIImageView *image;
@end

//GridCell.m

#import "GridCell.h"

@implementation GridCell

@synthesize image,lblName;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        UIImage *bg = [UIImage imageNamed:@"borderUIimgLg.png"];

        UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,frame.size.width,frame.size.width)];
        [bgImage setimage:bg];
        [bgImage setContentMode:UIViewContentModeScaleAspectFill];
         NSLog(@"BG Image size %f,%f",bgImage.frame.size.width,bgImage.frame.size.height);


        UIImageView *contentimage = [[UIImageView alloc] initWithFrame:CGRectMake(2.0,2.0,frame.size.width-4.0,frame.size.width-4.0)];
        [contentimage setContentMode:UIViewContentModeScaleAspectFill];
        [contentimage setClipsToBounds:YES];
        self.image = contentimage;

        [self.contentView addSubview:self.image];

        [self.contentView addSubview:bgImage];

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2.0,frame.size.width - 4.0,21.0)];
        [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:11.0]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
        self.lblName = label;
        [self.contentView addSubview:self.lblName];
    }
    return self;
}

UICollectionViewCell的大小为67 x 100,因此在代码中,bgImage应始终为67 x 67,其原点为(0,0),而contentimage应为(0,63),63).通过调试,似乎是正确的.但是,conentimage总是比bgImage大.但是图像的原始尺寸为80 x 80.我试过setClipToBounds,
在cell.ContentView或imageView上使用setContentviewmode,但都不起作用.

附上有关该问题的屏幕截图.

任何帮助表示赞赏.

解决方法

你在另一个上使用2次frame.size.width而不是frame.size.height

编辑,试试这个:

在单元格中,使用initWithFrame方法时,请使用单元格的self.bounds属性.如果在边框内初始化imageview,则使用CGRectInset方法初始化具有较小边界的imageview,并将imageviews center设置为与单元格的contentview相同

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...