2012-02-14 10 views
7

興味深い問題があります。私は1つのUIScrollViewを持っていて、イメージを持つUIImageViewは1つしかありません。私はズームとパンのためにこれが必要です。 ボタンのタッチで、UIImageView内の画像(UIScrollView内にある、言及したようなもの)を次の1行に置き換えます。iOSのUIScrollViewのUIImageViewでイメージを正しく置き換える方法

は、これまでのところ私はそのようにこれをやった:

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

私は常にnilにself.imageViewを設定した理由は、私は次の画像よりも、それをしない場合は、拡大縮小や、以前のようにズームされていることです1つはあった。

しかし、私はそれがメモリ効率のための良い解決策だとは思わない。

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

UPDATE 2 ちょうど私が間違って何をやっていた考え出しました。私は最小ズームスケールをリセットしませんでした。

[self.scrollView setMinimumZoomScale:1.0]; 

これで機能します。

答えて

7

UIScrollView

であなたのscrollviewのzoomScaleに設定1.0 zoomScaleと呼ばれるプロパティがあります。古いUIImageViewを解放し、毎回新しいUIImageViewを割り当てる必要はありません。画像を直接画像ビューに設定し、UIScrollViewのzoomScaleを1.0に設定することができます。

+0

これは適切なソリューションであるImageViewのイメージを変更する前に0。ありがとう! –

+1

これは実際には機能しません。 UIImageViewは前の画像からサイズを取得していますが間違っています。どうすればUIImageViewをリセットすることもできますか? –

+1

'zoomScale'を1に設定してイメージを設定しようとしましたか? – Ilanchezhian

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

、その後、あなたはあなたのScrollViewのためにコンテンツのサイズを設定することができます。

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

は、という理由だけで一つのことを明確にしたいです同じ問題に遭遇しました。最小ズームスケールを1にリセットしました。

[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; 
} 
関連する問題