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

如何在iOS上的UIScrollView中正确替换UIImageView中的图像,ios,uiscrollview,replace,uiimageview,uiimage,Ios,Uiscrollview,Replace,Uiimageview,Uiimage,我有一个有趣的问题。我有一个UIScrollView,里面只有一个带有图像的UIImageView。我需要这个来缩放和平移。 好啊按一下按钮,我想用行中的下一个替换UIImageView中的图像(在UIScrollView中,如前所述) 到目前为止,我是这样做的: self.imageView = nil; self.imageView = [[UIImageView alloc] initWithImage:nextImage]; 我总是将self.imageView设置为nil的原因是,如

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

现在它工作了

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

中有一个名为
zoomScale
的属性


将scrollview的缩放比例设置为1.0。无需每次都释放旧的UIImageView并分配新的UIImageView。您可以直接将图像设置为imageview,并将UIScrollView的缩放比例设置为1.0。

每次创建新对象时都使用此代码,但以前的UIImageView保留在UIScrollView上。这在内存使用方面是不高效的

使用下一个代码:

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

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

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

我只想澄清一件事,因为我遇到了相同的问题:在更改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;
}
-(无效)centerScrollViewContents{
CGSize boundsSize=self.scrollView.bounds.size;
CGRect contentsFrame=self.imageView.frame;
if(contentsFrame.size.width
实际上这不起作用。UIImageView从错误的上一个图像获取大小。如何重置UIImageView?在将
zoomScale
设置为1后,您是否尝试过将图像设置为1?同样,如果前一个图像大于当前图像,则此设置不正确。否则,图像不会填充整个ScrollView…好的,在这种情况下,使用@InfogI建议的新图像大小设置imageview的框架。我们在这个问题上苦苦挣扎了2天。谢谢你,伙计。我真的很感激。在更新之前重置缩放就成功了。再次非常感谢!
[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;
}