【问题标题】:UIImageView show image out of bounds in a UICollectionViewCell sub classUIImageView 在 UICollectionViewCell 子类中显示超出范围的图像
【发布时间】:2012-09-26 20:04:47
【问题描述】:

我必须创建一个自定义 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, 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, 0,63,63)。通过调试,它似乎是正确的。但是,conentimage 总是比 bgImage 大。图像的原始尺寸为 80 x 80。我试过 setClipToBounds, 在 cell.ContentView 或 imageView 上设置内容视图模式,但都不起作用。

附上关于该问题的截图。

感谢任何帮助。

【问题讨论】:

  • 我也尝试在一个笔尖和情节提要中布局所有元素,得到相同的结果。

标签: objective-c uiimageview uiimage ios6 uicollectionview


【解决方案1】:

现在可能已经找到答案了,试试吧:

contentImage.clipsToBounds = YES;

【讨论】:

    【解决方案2】:

    您使用的是 frame.size.width 的 2 倍,而不是另一倍的 frame.size.height

    编辑,试试这个:

    在单元格中,使用 initWithFrame 方法时,使用单元格的 self.bounds 属性。 如果您在边框内初始化图像视图,请使用 CGRectInset 方法以较小的边界初始化图像视图并将图像视图中心设置为与单元格的内容视图相同

    【讨论】:

    • 这实际上是我想要的,让图像具有相等的宽度和高度,与单元格的宽度相同。
    • 它有效。非常感谢。 :) 我将UIImageView *contentImage = [[UIImageView alloc] initWithFrame:CGRectMake(2.0, 2.0, frame.size.width-4.0, frame.size.width-4.0)]; 更改为UIImageView *contentImage = [[UIImageView alloc] initWithFrame: CGRectInset(bgImageView.bounds, 6, 6)];,神奇的事情发生了。我需要弄清楚这两者有什么区别。 4.0 或 6.0 无所谓,只有 6.0 最适合我的应用。
    【解决方案3】:

    尝试将 UIViewContentModeScaleAspectFill 更改为 UIViewContentModeScaleAspectFit。

    【讨论】:

    • 我尝试了填充和拟合,但没有运气。刚刚又试了一次,不行:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 2013-03-12
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多