2012-02-14 50 views
7

我有一个有趣的问题。我有一个UIScrollView和只有一个UIImageView和图像。我需要这个缩放和平移。 好的。在一个按钮触摸我想在UIImageView(这是在UIScrollView,像一个提到)与下一个在线取代图像。如何正确替换iOS中UIScrollView中的UIImageView中的图像

到目前为止,我已经做到了这一点像:

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

,我总是设置self.imageView为零的原因是,如果我不这样做,比下缩放图像或缩放像前一个是。

但我不认为这是解决内存效率的好方法。

是否有任何其他方式来设置UIImageView的新图像与“重置”选项的缩放,比例等......?

UPDATE: 仍然无法正常工作代码:

[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]; 

现在,它的工作原理!

回答

7

有一个叫zoomScale财产UIScrollView

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

+0

这是正确的解决方案。谢啦! – 2012-02-14 10:49:17

+1

其实这是行不通的。 UIImageView从以前的图像中获取大小,这是错误的。我怎样才能重置UIImageView? – 2012-02-14 11:02:40

+1

您是否在将zoomScale设置为1后尝试设置图像? – Ilanchezhian 2012-02-14 11:06:38

1

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

0

乳清你使用这个代码,每次你创建新的对象,但以前的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)]; 

,然后,你可以为你滚动型设置内容大小:

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

只是想弄清楚一两件事,因为我遇到了同样的问题:将最小缩放比例重置为1。0之前,改变所述的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]; 

和用于定心图像:

- (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; 
} 
相关问题