在自定义UICollectionViewCell的UIScrollView中调整UIImageView

时间:2014-03-26 11:51:34

标签: ios iphone uiscrollview uiimageview uiimage

我正在使用自定义UICollectionViewCell,单元格包含一个大小为(320,145)的UIImageView,但我需要将400 x 400的图像显示在其中,所以我试图使用UIScrollView显示整个图像而不裁剪它,即用户可以滚动查看部分中的整个图像。

我在scrollView中添加了我的imageView,并为scrollView提供了适当的帧大小和内容大小,如下面的代码所示。但是静止图像看起来不正常,我可以看到图像变小以适应屏幕,我也可以看到整个图像并滚动,但我想看到完整的图像,即400 x 400。

UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 145)];
[imgScrollView setScrollEnabled:YES];
[imgScrollView setClipsToBounds:YES];
[imgScrollView addSubview:_albumImageView];
[imgScrollView setBackgroundColor:[UIColor yellowColor]];

[imgScrollView setContentSize:CGSizeMake(400, 400)];  /// my _albumImageView is of 400 x 400 

_albumImageView.contentMode = UIViewContentModeScaleToFill;
_albumImageView.clipsToBounds = YES;

[self.contentView addSubview:imgScrollView];

以上代码的输出如下所示 enter image description here

但是我希望它如下面的屏幕截图,滚动启用。(图像可能会有不同的相同大小) enter image description here

2 个答案:

答案 0 :(得分:0)

我认为是因为您已将contentMode属性设置为UIViewContentModeScaleToFill

_albumImageView.contentMode = UIViewContentModeScaleToFill;

将导致图像调整大小以填充帧(320x145)。


修改

此外,_albumImageView中的图像是可调整大小的 UIImage吗?如果没有,请使用

调整其大小
UIImage *img = [albumImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];

将其添加到_albumImageView

之前

答案 1 :(得分:0)

试试这个

_albumImageView.contentMode = UIViewContentModeScaleAspectFill;