Ios AQGridview图像在向下滚动后正在调整大小
我正在使用AQGridview在Gridview中显示我的图像 一切正常,但当我向下滚动时,我的图像会调整大小。(见截图) 这是我在ViewController中执行的操作Ios AQGridview图像在向下滚动后正在调整大小,ios,iphone,objective-c,aqgridview,Ios,Iphone,Objective C,Aqgridview,我正在使用AQGridview在Gridview中显示我的图像 一切正常,但当我向下滚动时,我的图像会调整大小。(见截图) 这是我在ViewController中执行的操作 - (AQGridViewCell *) gridView: (AQGridView *) aGridView cellForItemAtIndex: (NSUInteger) index { static NSString * PlainCellIdentifier = @"PlainCellIdentifier"
- (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;
}
-(id)initWithFrame:(CGRect)帧重用标识符:(NSString*)aReuseIdentifier
{
self=[super initWithFrame:frame reuseIdentifier:aReuseIdentifier];
if(self==nil)
回报率(零);
_imageView=[[UIImageView alloc]initWithFrame:CGRectZero];
[self.contentView addSubview:_imageView];
回归(自我);
}
-(void)setImage:(NSString*)strurl
{
[\u imageView setImageWithURL:[NSURL URLWithString:strurl]
占位符图像:[UIImage ImageName:@“菜单”]];
[self-setNeedsLayout];
}
-(无效)布局子视图
{
NSLog(“布局子视图”);
[超级布局子视图];
CGSize imageSize=\u imageView.image.size;
CGRect frame=_imageView.frame;
CGRect bounds=self.contentView.bounds;
如果((imageSize.width我已设置更改此项:
frame.size.width = floorf(imageSize.width * ratio);
frame.size.height = floorf(imageSize.height * ratio);
对此
frame.size.width = 75;
frame.size.height = 75;
为什么要手动调整图像视图的大小?您只需将图像视图居中,然后使用“将内容模式设置为UIViewContentModeScaleSpectFit
或类似的设置即可。