我正在使用 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;
}