如何在 iOS 上正确替换 UIScrollView 中 UIImageView 中的图像

发布于 2025-01-04 22:59:03 字数 1220 浏览 5 评论 0 原文

我有一个有趣的问题。我有一个 UIScrollView ,里面只有一个带有图像的 UIImageView 。我需要这个来缩放和平移。 好的。在触摸按钮时,我想用下一个图像替换 UIImageView 中的图像(如前面提到的,它位于 UIScrollView 中)。

到目前为止,我已经这样做了:

self.imageView = nil;
self.imageView = [[UIImageView alloc] initWithImage:nextImage];

我总是将 self.imageView 设置为 nil 的原因是,如果我不这样做,那么下一张图像会像前一张图像一样缩放或缩放。

但我不认为这对于内存效率来说是一个很好的解决方案。

有没有其他方法可以通过“重置”选项在 UIImageView 上设置新图像以进行缩放、缩放等...?

更新: 仍然不起作用的代码:

[self.scrollView setZoomScale:1.0];
[self.imageView setImage:photoImage];
[self.imageView setFrame:rect];
[self.scrollView setContentSize:[self.imageView frame].size];
CGFloat minZoomScale = [self.scrollView frame].size.width / [self.imageView frame].size.width;
if (minZoomScale < [self.scrollView frame].size.height / [self.imageView frame].size.height) {
    minZoomScale = [self.scrollView frame].size.height / [self.imageView frame].size.height;
}
[self.scrollView setMinimumZoomScale:minZoomScale];
[self.scrollView setZoomScale:[self.scrollView minimumZoomScale]];

更新2 刚刚弄清楚我做错了什么。我没有重置最小缩放比例。

[self.scrollView setMinimumZoomScale:1.0];

现在可以了!

I have an interesting problem. I have one UIScrollView and inside only one UIImageView with image. I need this for zooming and panning.
Ok. On a button touch I want to replace image within UIImageView (which is within UIScrollView, like a mentioned) with the next one in line.

So far I've done this like that:

self.imageView = nil;
self.imageView = [[UIImageView alloc] initWithImage:nextImage];

The reason that I always set self.imageView to nil is that if I don't do that than the next image is scaled or zoomed like the previous one was.

But I don't think thats fine solution for memory efficiency.

Is there any other way to set new image on UIImageView with "reset" option for zooming, scale, etc... ?

UPDATE:
The code that still does not work:

[self.scrollView setZoomScale:1.0];
[self.imageView setImage:photoImage];
[self.imageView setFrame:rect];
[self.scrollView setContentSize:[self.imageView frame].size];
CGFloat minZoomScale = [self.scrollView frame].size.width / [self.imageView frame].size.width;
if (minZoomScale < [self.scrollView frame].size.height / [self.imageView frame].size.height) {
    minZoomScale = [self.scrollView frame].size.height / [self.imageView frame].size.height;
}
[self.scrollView setMinimumZoomScale:minZoomScale];
[self.scrollView setZoomScale:[self.scrollView minimumZoomScale]];

UPDATE 2
Just figured out what I was doing wrong. I did not reset the minimum zoom scale.

[self.scrollView setMinimumZoomScale:1.0];

Now it works!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一生独一 2025-01-11 22:59:03

zoomScale 的属性"noreferrer">UIScrollView

将 1.0 设置为滚动视图的 ZoomScale。无需每次释放旧的 UIImageView 并分配新的 UIImageView。您可以直接将图像设置为imageview,并将UIScrollView的zoomScale设置为1.0。

There is a property called zoomScale in UIScrollView

Set 1.0 to the zoomScale of your scrollview. No need to release the old UIImageView and to allocate a new UIImageView everytime. You can directly set the image to the imageview and set the zoomScale of the UIScrollView to 1.0.

伤感在游骋 2025-01-11 22:59:03

只是想澄清一件事,因为我遇到了同样的问题:在更改 imageView 的图像之前将最小缩放比例重置为 1.0:

[self.scrollView setMinimumZoomScale:1.0];
[self.scrollView setZoomScale:1.0];
[self.imageView setImage:photoImage];
[self.photoView setFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), photoImage.size=photo.size}];
[self.scrollView setContentSize:[self.imageView frame].size];
CGFloat minZoomScale = MIN([self.scrollView frame].size.width / [self.imageView frame].size.width,  [self.scrollView frame].size.height / [self.imageView frame].size.height)
[self.scrollView setMinimumZoomScale:minZoomScale];
[self.scrollView setMaximumZoomScale:1.0];
[self.scrollView setZoomScale:[self.scrollView minimumZoomScale]];
[self centerScrollViewContents];

以及将图像居中:

- (void)centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.imageView.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.imageView.frame = contentsFrame;
}

Just want to make one thing clear because I ran into the same problem: reset the minimum zoom scale to 1.0 BEFORE changing the image of the imageView:

[self.scrollView setMinimumZoomScale:1.0];
[self.scrollView setZoomScale:1.0];
[self.imageView setImage:photoImage];
[self.photoView setFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), photoImage.size=photo.size}];
[self.scrollView setContentSize:[self.imageView frame].size];
CGFloat minZoomScale = MIN([self.scrollView frame].size.width / [self.imageView frame].size.width,  [self.scrollView frame].size.height / [self.imageView frame].size.height)
[self.scrollView setMinimumZoomScale:minZoomScale];
[self.scrollView setMaximumZoomScale:1.0];
[self.scrollView setZoomScale:[self.scrollView minimumZoomScale]];
[self centerScrollViewContents];

and for centring the image:

- (void)centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.imageView.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.imageView.frame = contentsFrame;
}
情魔剑神 2025-01-11 22:59:03

您只需使用scrollView对象的zoomScale属性并将其设置为1.0每次更改图像。这可能会在加载新图像之前进行。这样,每次您只想更改图像时,就不必分配并初始化一个新的 UIIMageVIew 对象。另外,您可以将此更改放入动画块中,使图像变化不是尖锐而是渐进的。

You can just use the zoomScale property of the scrollView object and set it to 1.0 everytime change the image. This'll probably go before loading the new image. This way you'll not have to alloc and init a new UIIMageVIew object each time you want to change just the image. Additionally you can put this change into an animation block to make the image change not sharp but gradual.

岁月流歌 2025-01-11 22:59:03

为什么你使用这段代码,每次创建新对象时,但以前的 UIImageView 保留在你的 UIScrollView 上。这对于内存使用效率不高。

使用下一个代码:

[self.imageView removeFromSuperview];
[self.imageView release];
self.imageView = [[UIImageView alloc] initWithImage: nextImage];
[self addSubview: imageView];

或者

[self.imageView setImage: nextImage];
[self.imageView setFrame: CGRectMake(0, 0, nextImage.size.width, nextImage.size.height)];

然后,您可以为 ScrollView 设置内容大小:

topScrollView.contentSize = CGSizeMake(nextImage.size.width, nextImage.size.height);

Whey you use this code, every time you create new object, but previous UIImageView stay on your UIScrollView. This is not efficient in memory usage.

Use next code:

[self.imageView removeFromSuperview];
[self.imageView release];
self.imageView = [[UIImageView alloc] initWithImage: nextImage];
[self addSubview: imageView];

or

[self.imageView setImage: nextImage];
[self.imageView setFrame: CGRectMake(0, 0, nextImage.size.width, nextImage.size.height)];

and then, you can set content size for you ScrollView:

topScrollView.contentSize = CGSizeMake(nextImage.size.width, nextImage.size.height);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文