xcode iPhone:如何仅缩放滚动视图中的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4365229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone: how to zoom only the image from scrollview
提问by yosh
I have this problem with ScrollView zoom. There are many articles about it but somehow I can't find proper explanation, tutorial or code snippet.
我对 ScrollView 缩放有这个问题。有很多关于它的文章,但不知何故我找不到正确的解释、教程或代码片段。
I have full screen scrollview and inside a fullscreen imageview. Inside imageview I load AspectFit images (they can be either vertical or horizontal, horizontal ones have black space above and under). When I make zoom I zoom not only the image but also the area around (the rest of imageview). Moreover, when I rotate simulator while in zoom the image goes to top-right corner instead of being centered like at start. Few pixels even go out of the screen.
我有全屏滚动视图和全屏图像视图。在 imageview 中,我加载 AspectFit 图像(它们可以是垂直的或水平的,水平的图像上下有黑色空间)。当我进行缩放时,我不仅会缩放图像,还会缩放周围的区域(imageview 的其余部分)。此外,当我在缩放时旋转模拟器时,图像会转到右上角,而不是像开始时那样居中。甚至很少有像素超出屏幕。
I would really like to zoom only image instead of whole imageview or at least have the image centered all the time when zoomed.
我真的很想只缩放图像而不是整个图像视图,或者至少在缩放时始终将图像居中。
Any help welcome..
欢迎任何帮助..
回答by yosh
I solved the problem, it's not the best code but works good for me for now, I will probably clean it a bit later. Posting here, maybe it will help someone :)
我解决了这个问题,它不是最好的代码,但现在对我有用,我可能会稍后清理它。在这里发帖,也许它会帮助某人:)
UIImage *img = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:someImage ofType:@"jpg"]];
CGFloat scale;
if (scrollView.frame.size.width < scrollView.frame.size.height) // portrait
scale = scrollView.frame.size.width / img.size.width;
if (scrollView.frame.size.width > scrollView.frame.size.height) // landscape
scale = scrollView.frame.size.height / img.size.height;
imgView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y,
img.size.width*scale, img.size.height*scale);
imgView.center = CGPointMake(scrollView.frame.size.width/2, scrollView.frame.size.height/2);
[imgView setImage:img];
[scrollView setZoomScale:scale];
回答by Ertebolle
From the PhotoScroller example; add this to layoutSubviews:
来自 PhotoScroller 示例;将此添加到 layoutSubviews:
// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = imageView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
imageView.frame = frameToCenter;
This won't help with zooming only the image but it'll keep it centered when you zoom it at least.
这对仅缩放图像无济于事,但至少在缩放时会使其居中。