9

我已经成功实现了放大,UIImageViewUIScrollView我遇到了一个让我恼火的奇怪问题。

基本上,当我放大图像时

在此处输入图像描述

我可以平移视图以实际滚动出图像边框,并留下一个黑色区域,如下所示: 在此处输入图像描述

当我放大更多时,我可以让黑色边框填满整个屏幕!同时在 iPhone 照片应用程序中,您无法缩小实际图像。我的实现有什么问题?

它看起来像这样:

UIImage *imageToDisplay = [UIImage imageWithData:tmpImageData];
imageViewMain.image = imageToDisplay;
imageViewMain.frame = CGRectMake(0, 0, scrollViewMain.frame.size.width, scrollViewMain.frame.size.height);
imageViewMain.contentMode = UIViewContentModeScaleAspectFit;

scrollViewMain.delegate = self;
scrollViewMain.minimumZoomScale = 1.0;
scrollViewMain.maximumZoomScale = 9.0;
scrollViewMain.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollViewMain.contentSize = imageViewMain.frame.size;

[self.view addSubview:scrollViewMain];
[scrollViewMain addSubview:imageViewMain];

我也实现了这个方法:

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imageViewMain;
}

提前致谢!

编辑:当我第一次加载视图时,我缩小 UIImageView 以适应滚动视图,图像看起来像这样:

在此处输入图像描述

4

2 回答 2

2

试试这个代码......在需要的地方更改一些代码......UIPinchGestureRecognizer用于你的UIImageView,然后在你UIScrollView喜欢的下面添加这个图像......

UIImageView *img = [[UIImageView alloc]init];
img.image = [UIImage imageWithData:tmpImageData];
img.userInteractionEnabled = YES;
img.frame = CGRectMake(210,100, 200, 200);/// define frame which you want...
[img sizeToFit];

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[img addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
[scrollViewMain addSubview:img];
[scrollViewMain bringSubviewToFront:img];
[img release]; 

只需将以下代码粘贴到您的 .m 文件中...

#pragma mark GestureRecognizer Methods
-(void)scale:(id)sender 
{
    UIView *imgTempGest = [sender view];
    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) 
    {
        lastScale = 1.0;
        return;
    }
    CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

    CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

    [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

    [imgTempGest setTransform:newTransform];

    lastScale = [(UIPinchGestureRecognizer*)sender scale];
}

这里对于 LastScale 只需在文件中定义该变量,.h如下所示...

CGFloat lastScale;

我希望这对你有帮助......

于 2013-03-22T12:32:15.517 回答
1

原始图像是否包含顶部和底部的边框?如果实际图像包含黑色顶部和底部黑色区域,您可以在那里使用相同的代码输出没有错误。并添加此代码,

scrollView.scrollEnabled = YES

也变了,

scrollView.contentSize = imageView.bounds.size;

您还提供了 9 的缩放比例。您需要这么多吗?将其更改为 3 或 4 并尝试。您的问题将得到解决

于 2013-03-22T13:32:54.573 回答