2

我对使用 UIScrollView 的捏缩放功能感到满意。但问题是滚动视图中图像的宽高比。

目前,我有这个,下图:

在此处输入图像描述

但我希望图像适合屏幕,如下图所示:

在此处输入图像描述

风景也有同样的行为。我怎样才能做到这一点?

下面是代码:

- (void)viewDidLoad
{
    UIImage *image = [UIImage imageWithData:appDelegate.selectedOriginalImage];
    imgView.image = image;
    CGSize imageSize = image.size;
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    imgView.frame = rect;
    scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
    scrollView.maximumZoomScale = 4.0;
    scrollView.minimumZoomScale = 1.0;
    scrollView.delegate = self;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    UIImage *image = [UIImage imageWithData:appDelegate.selectedOriginalImage];
    CGSize imageSize = image.size;
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    imgView.frame = rect;

    [scrollView setContentOffset:CGPointMake(0, 0)]; 

    return YES;
}
4

1 回答 1

4

您将 UIImageView 设置为图像的原始大小,而不是包含它的 UIScrollView 的大小。

- (void)viewDidLoad
{
    UIImage *image = [UIImage imageWithData:appDelegate.selectedOriginalImage];
    imgView.image = image;

    imgView.frame = scrollView.bounds;
    [imgView setContentMode:UIViewContentModeScaleAspectFit];
    scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
    scrollView.maximumZoomScale = 4.0;
    scrollView.minimumZoomScale = 1.0;
    scrollView.delegate = self;
}

记得在 viewToZoom 委托方法上返回 imgView

于 2012-12-06T15:04:25.060 回答