UIScrollView 挤压 UIImageView 图像
Posted
技术标签:
【中文标题】UIScrollView 挤压 UIImageView 图像【英文标题】:UIScrollView squashes UIImageView image 【发布时间】:2012-11-10 17:47:51 【问题描述】:我在ViewDidLoad
中有以下代码,它会挤压我的 UIImage/UIImageView。我希望图像填充 UIScrollView。
[super viewDidLoad];
_imageHolder = [[UIImageView alloc]initWithFrame:self.view.frame];
_scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
[_scrollView setBackgroundColor:[UIColor purpleColor]];
// Tell the scroll view the size of the contents
self.scrollView.contentSize = self.view.frame.size;
self.scrollView.delegate = self;
self.scrollView.scrollEnabled = YES;
FfThumbnailData* thumbData = [self.arrayOfImages objectAtIndex:self.thisImageIndex];
UIImage* fullSizedImage = [[UIImage alloc]initWithContentsOfFile:thumbData.path];
[self.imageHolder setImage:fullSizedImage];
float x = fullSizedImage.size.width;
float y = fullSizedImage.size.height;
CGRect rect = CGRectMake(0, 0, x, y);
NSLog(@"Size of image: %@", NSStringFromCGRect(rect));
//
//[self.imageHolder setImage:[UIImage imageNamed:@"P1020486.png"]];
[self.scrollView addSubview:_imageHolder];
[self.view addSubview:self.scrollView];
_imageHolder.contentMode = UIViewContentModeScaleAspectFit;
为了清晰起见,我将 UIScrollView 的背景设置为紫色:
http://i1219.photobucket.com/albums/dd427/Dave_Chambers/IMG_0315.png
我应该补充一点,在我放大然后缩小之后,图像被正确放置在滚动视图中并且没有被压扁。
奇怪的是,注释行 [self.imageHolder setImage:[UIImage imageNamed:@"P1020486.png"]];
正确显示了虚拟图像 P1020486.png。
另外,当我下载我的应用数据时,图像看起来正确,并且确实是 iPad 图像的正确大小 - 1936, 2592 - 我的 NSLog 行 NSLog(@"Size of image: %@", NSStringFromCGRect(rect));
报告的大小
此外,在我需要 UIScrollView(用于在全尺寸视图控制器中缩放照片)之前,我访问相同的图像并使用以下代码通过动画从一个视图控制器显示到另一个视图控制器,并且它可以正确显示。至关重要的是,这第二部分代码缺少 UIScollview。
-(void)animateTransition:(int)buttonInPositon
@try
FfThumbnailData* thumbData = [self.images objectAtIndex:buttonInPositon];
UIImage* fullSizedImage = [[UIImage alloc]initWithContentsOfFile:thumbData.path];
fullSize = [[FullSizeViewController alloc]init];
fullSize.imageHolderGhost.contentMode = UIViewContentModeScaleAspectFit;
fullSize.arrayOfImages = self.images;
fullSize.imageToPresent = fullSizedImage;
fullSize.numberOfImages = self.images.count;
fullSize.thisImageIndex = buttonInPositon;
[fullSize.imageHolderGhost setImage:fullSizedImage];
float screenWidth = self.view.bounds.size.width;
float screenHeight = self.view.bounds.size.height;
NSLog(@"ButtonFrame: %@", NSStringFromCGRect(buttonFrame));
[fullSize.view setFrame:CGRectMake(buttonFrame.origin.x, buttonFrame.origin.y, buttonFrame.size.width, buttonFrame.size.height-44)];
NSLog(@"ButtonFrame: %@", NSStringFromCGRect(fullSize.view.frame));
[self.view addSubview:fullSize.view];
fullSize.view.backgroundColor = [UIColor clearColor];
[UIView animateWithDuration:0.9 delay:0 options:UIViewAnimationCurveEaseIn animations:^
[fullSize.view setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
completion:^(BOOL finished)
[[self navigationController] pushViewController:fullSize animated:NO];
self.navigationController.navigationBarHidden = YES;
];
@catch (NSException *exception)
NSLog(@"%@", exception);
为了完整起见,带有 UIScrollView 的代码在一个 VC 中具有以下方法:
-(void)presentWithScrollview:(int)buttonInPositon
FfThumbnailData* thumbData = [self.images objectAtIndex:buttonInPositon];
UIImage* fullSizedImage = [[UIImage alloc]initWithContentsOfFile:thumbData.path];
fullSize = [[FullSizeViewController alloc]init];
fullSize.arrayOfImages = self.images;
fullSize.imageToPresent = fullSizedImage;
fullSize.numberOfImages = self.images.count;
fullSize.thisImageIndex = buttonInPositon;
[[self navigationController] pushViewController:fullSize animated:NO];
self.navigationController.navigationBarHidden = YES;
在目标 VC 中,以下方法可能与我的问题有关(ViewDidLoad
粘贴在此问题的顶部):
- (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
// Set up the minimum & maximum zoom scales
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
//
self.scrollView.minimumZoomScale = minScale;
//Can set this bigger if needs be - match iPhoto
self.scrollView.maximumZoomScale = 2.0f;
[self centerScrollViewContents];
- (void)centerScrollViewContents
CGSize boundsSize = self.scrollView.bounds.size;
//NSLog(@"boundsSize: %@", NSStringFromCGSize(boundsSize));
CGRect contentsFrame = self.imageHolder.frame;
//NSLog(@"contentsFrame: %@", NSStringFromCGRect(contentsFrame));
if (contentsFrame.size.width < boundsSize.width)
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
else
contentsFrame.origin.x = 0.0f;
if (contentsFrame.size.height < boundsSize.height)
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
else
contentsFrame.origin.y = 0.0f;
self.imageHolder.frame = contentsFrame;
#pragma mark - UIScrollViewDelegate
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
// Return the view that we want to zoom
return self.imageHolder;
//return self.scrollViewBar;
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
// The scroll view has zoomed, so we need to re-center the contents
[self centerScrollViewContents];
对此的任何帮助都会很棒。谢谢
【问题讨论】:
【参考方案1】:所以,我自己找到了解决方案。出于某种原因,正如我在对问题的更新中所写的那样,任何放大,然后缩小图像都可以解决图像问题并正确呈现。我将这两行添加到viewWillAppear
,上面[self centerScrollViewContents];
self.scrollView.zoomScale = 2.0;
self.scrollView.zoomScale = 1.0;
问题解决了
【讨论】:
以上是关于UIScrollView 挤压 UIImageView 图像的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollView 内的 MPMoviePlayerController 缩放问题
在 UIScrollView 内的 UIImageView 上启用 GestureRegnizer
在 UIScrollView 中放大多个 UIImageView