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

标签 ios uiscrollview replace uiimageview uiimage

我有一个有趣的问题。我有一个 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];

现在可以了!

最佳答案

UIScrollView 中有一个名为zoomScale 的属性

将 1.0 设置为 ScrollView 的 zoomScale。无需每次释放旧的 UIImageView 并分配新的 UIImageView。可以直接将图片设置为imageview,并将UIScrollView的zoomScale设置为1.0。

关于ios - 如何在 iOS 上的 UIScrollView 中正确替换 UIImageView 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9274523/

相关文章:

android - 使用 Cordova 的跨平台菜单

ios - 按动画按钮时不动画

ios - PDF 在 UIScrollView 中缩放

python - 为什么在未使用正确参数请求时,replace() 函数会返回语法编辑后的结果?

c# - 查找并替换所有以 # 开头的单词,并将带标签的文本包装在 HTML 中

iOS:重新加载 UIImage(name:...) 的性能

ios - Xcode 中安装 SQLite 报错

ios - 如何打开从一个 UIWebView 到另一个 UIWebView 的链接?

ios - UIScrollView contentInset 属性有什么用?

mysql - 查询从列内容中删除多个字符