【问题标题】:How to properly replace image in UIImageView in UIScrollView on iOS如何在 iOS 上的 UIScrollView 中正确替换 UIImageView 中的图像
【发布时间】:2012-02-14 09:32:28
【问题描述】:

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

现在可以了!

【问题讨论】:

    标签: ios uiscrollview replace uiimageview uiimage


    【解决方案1】:

    UIScrollView中有一个名为zoomScale的属性

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

    【讨论】:

    • 其实这行不通。 UIImageView 从上一个错误的图像中获取大小。我怎样才能重置 UIImageView?
    • 设置zoomScale为1后是否尝试设置图片?
    • 如果之前的图像比当前的图像大,也不是这样。否则图像不会填满整个 ScrollView...
    • 好的,在这种情况下,使用@Infog 建议的新图像大小设置图像视图的框架
    【解决方案2】:

    只想说清楚一件事,因为我遇到了同样的问题:在更改 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;
    }
    

    【讨论】:

    • 我在这个问题上苦苦挣扎了 2 天。非常感谢伙计。我真的很感激。在更新之前重置缩放就可以了。再次感谢!
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      您每次创建新对象时都使用此代码,但之前的 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-26
        • 2012-09-14
        • 1970-01-01
        • 2019-01-11
        • 1970-01-01
        • 2012-08-16
        相关资源
        最近更新 更多