我在这里找到了其他一些答案,但它们似乎没有应用/工作,所以我认为这可能是由于我的特定代码。
在我的应用程序中,我有一系列UIImages,实际上缩小了iPhone相机拍摄的照片。
我试图在scrollView中显示UIImage,正确缩放以显示整个图像。
这是我的代码:
scrollView.pagingEnabled = YES;
scrollView.clipsToBounds = NO;
CGFloat contentOffset = 0.0f;
pageControl.numberOfPages = [imageArray count];
pageControl.currentPage = 0;
for(UIImage *image in imageArray)
{
NSLog(@"image width = %f, image height = %f", image.size.width, image.size.height);
CGRect imageViewFrame = CGRectMake(contentOffset, 0, scrollView.frame.size.width, scrollView.frame.size.height);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
[imageView setImage:image];
[imageView setClipsToBounds:YES];
[imageView setContentMode:UIViewContentModeScaleAspectFill]; //I've tried others, but nothing seems to show the entire image
[scrollView addSubview:imageView];
[imageView release];
contentOffset += scrollView.frame.size.width;
[scrollView setContentSize:CGSizeMake(contentOffset, 0)];
}
请注意,这是一个面向肖像的应用程序,图像本身是纵向拍摄的。
NSLog输出:
image width = 387.200012, image height = 518.400024
我看到的图像显示在UIImageView上(在滚动视图内)是图像中心的放大图像,无论我将内容模式更改为什么。
我在这里做错了什么?谢谢!
答案 0 :(得分:1)
答案就是我之前评论的内容:UIImage的缩放因子导致ContentMode无法正常工作。或者至少,让它看起来不起作用。
答案 1 :(得分:0)
我知道这已经被问到并得到了回答,但最近这个问题似乎还有另一个原因。从 tvOS 9.1 开始,设置adjustsImageWhenAncestorFocused
似乎会使contentMode
过时(可能是错误)UIImageView
。我希望这有助于某人。