gpt4 book ai didi

ios - 如何缩放填满屏幕的 UIImageView

转载 作者:行者123 更新时间:2023-11-28 18:39:23 24 4
gpt4 key购买 nike

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

接口(interface):

#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

最佳答案

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

self.scrollView.maximumZoomScale = 1.0f;

尝试将 2.0f 或更大的值放在那里。

关于ios - 如何缩放填满屏幕的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13310882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com