8

画像と画像の名前を表示するには、カスタマイズした 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 です。cell.ContentViewまたはimageViewのいずれかでsetClipToBounds、setContentViewModeを試しましたが、どれも機能しません。

問題に関するスクリーンショットが添付されています。範囲外の UIImage ビューの画像

どんな助けでも大歓迎です。

4

3 に答える 3

1

もう一方の frame.size.height の代わりに 2 回 frame.size.width を使用しています

編集して、これを試してください:

セルで initWithFrame メソッドを使用する場合は、セルの self.bounds プロパティを使用します。境界内のイメージビューを初期化する場合は、CGRectInset メソッドを使用してイメージビューをより小さな境界で初期化し、イメージビューの中心をセルのコンテンツビューと同じに設定します

于 2012-10-05T22:33:28.283 に答える
0

UIViewContentModeScaleAspectFit を UIViewContentModeScaleAspectFit に変更してみてください。

于 2012-10-05T16:57:44.330 に答える