向下滚动后,AQGridview图像正在调整大小

时间:2013-11-28 13:52:13

标签: ios iphone objective-c aqgridview

我正在使用AQGridview在Gridview中显示我的图像。

每一个都可以正常工作,但是当我向下滚动时,我的图像会被调整大小。 (见截图)

这就是我在ViewController中所做的事情

- (AQGridViewCell *) gridView: (AQGridView *) aGridView cellForItemAtIndex: (NSUInteger) index
{
    static NSString * PlainCellIdentifier = @"PlainCellIdentifier";
    AQGridViewCell * cell = nil;
    ImageDemoGridViewCell * plainCell = (ImageDemoGridViewCell *)[aGridView dequeueReusableCellWithIdentifier: PlainCellIdentifier];
    if ( plainCell == nil )
    {
        plainCell = [[ImageDemoGridViewCell alloc] initWithFrame: CGRectMake(0.0, 0.0, 75, 75)
                                                 reuseIdentifier: PlainCellIdentifier];
    }
    Photo *photo = [_imageNames objectAtIndex:index];
    plainCell.image = [NSString stringWithFormat:@"http://sportifun2014.sanmax.be/images/albums_photos/big/%@",photo.pho_filename];
    cell = plainCell;

    return cell ;
}
- (CGSize) portraitGridCellSizeForGridView: (AQGridView *) aGridView
{
    return ( CGSizeMake(75, 89.9) );
}

这是我的自定义单元类

- (id) initWithFrame: (CGRect) frame reuseIdentifier: (NSString *) aReuseIdentifier
{
    self = [super initWithFrame: frame reuseIdentifier: aReuseIdentifier];
    if ( self == nil )
        return ( nil );
    _imageView = [[UIImageView alloc] initWithFrame: CGRectZero];
    [self.contentView addSubview: _imageView];

    return ( self );
}
- (void) setImage: (NSString *) strurl
{

   [_imageView setImageWithURL:[NSURL URLWithString:strurl]
               placeholderImage:[UIImage imageNamed:@"menu"]];

       [self setNeedsLayout];
}
- (void) layoutSubviews
{
    NSLog(@"layoutSubviews");
    [super layoutSubviews];

    CGSize imageSize = _imageView.image.size;
    CGRect frame = _imageView.frame;
    CGRect bounds = self.contentView.bounds;

    if ( (imageSize.width <= bounds.size.width) &&
        (imageSize.height <= bounds.size.height) )
    {
        return;
    }

    // scale it down to fit
    CGFloat hRatio = bounds.size.width / imageSize.width;
    CGFloat vRatio = bounds.size.height / imageSize.height;
    CGFloat ratio = MAX(hRatio, vRatio);

    frame.size.width = floorf(imageSize.width * ratio);
    frame.size.height = floorf(imageSize.height * ratio);
    frame.origin.x = floorf((bounds.size.width - frame.size.width) * 0.5);
    frame.origin.y = floorf((bounds.size.height - frame.size.height) * 0.5);
    _imageView.frame = frame;
}

BEFORE AFTER

1 个答案:

答案 0 :(得分:0)

我已经改变了这个:

  frame.size.width = floorf(imageSize.width * ratio);
    frame.size.height = floorf(imageSize.height * ratio);

到这个

  frame.size.width = 75;
    frame.size.height = 75;