如何缩放填充屏幕的 UIImageView

Posted

技术标签:

【中文标题】如何缩放填充屏幕的 UIImageView【英文标题】:How to Zoom a UIImageView that fills the screen 【发布时间】:2012-11-09 15:27:18 【问题描述】:

我只是想缩放我在 UIScrollView 上的 UIImageView 上设置的照片。也许是因为图像与屏幕大小相同,但在我下面的代码中既没有调用viewForZoomingInScrollView,也没有调用scrollViewDidZoom。请帮我解决这个问题,以便我可以放大图像。谢谢:

界面:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView* scrollView;
@property (strong, nonatomic) UIImageView *imageHolder;

@end

实施:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad

[super viewDidLoad];

// Tell the scroll view the size of the contents
self.scrollView.contentSize = self.view.frame.size;

self.scrollView.delegate = self;
self.scrollView.scrollEnabled = YES;

_imageHolder = [[UIImageView alloc]initWithFrame:self.view.frame];
_scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];

[self.imageHolder setImage:[UIImage imageNamed:@"P1020486.png"]];

[self.scrollView addSubview:_imageHolder];
[self.view addSubview:self.scrollView];

//Keep the proportion and fit the screen, not filling it --> when we rotate, resizes image correctly:
_imageHolder.contentMode = UIViewContentModeScaleAspectFit;


- (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;
self.scrollView.maximumZoomScale = 1.0f;

[self centerScrollViewContents];


- (void)centerScrollViewContents 
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.imageHolder.frame;

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];


- (void)didReceiveMemoryWarning

[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.


@end

【问题讨论】:

【参考方案1】:

我没有时间测试它,但我认为问题在于这一行:

self.scrollView.maximumZoomScale = 1.0f;

试着放 2.0f 或更大的东西。

【讨论】:

感谢您的建议。您的建议似乎合乎逻辑,但遗憾的是它仍然无法放大。 嗨,Alois,虽然下面 CodeBySteveZ 的回答为我解决了这个问题,但您的回答也是解决方案的一部分,因为 1.0f 不好。再次感谢。【参考方案2】:

从快速查看您的代码来看,您似乎在初始化之前在 scrollView 上设置了属性:

self.scrollView.contentSize = self.view.frame.size;

self.scrollView.delegate = self;

self.scrollView.scrollEnabled = YES;

_scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];

如果您颠倒顺序,它应该可以工作。

【讨论】:

是的!非常感谢。这对我来说非常愚蠢,但我们走了。现在它起作用了。干杯

以上是关于如何缩放填充屏幕的 UIImageView的主要内容,如果未能解决你的问题,请参考以下文章

Andengine,缩放图像以填充屏幕大小,而不保持纵横比

IOS - 将背景图像缩放到不同的屏幕尺寸并将标签放在固定位置上

定位和缩放启动屏幕图像

如何相对缩放用户控件的大小?

使 UIImageView 及其 UIImage 按比例缩放而无需额外的填充

使用 CGAffineTransform 缩放 tableView 单元格以覆盖整个屏幕