2011-07-08 14 views
0

イメージがセンターの幅/高さで表示されるように(iphone画面に関して)イメージを中央に配置しようとしています...私の場合、私のビューは画像自体のピクセルポイント0,0にロードされ、ズームアウトすると、画像は画面/ビューの中心の中央ではなく左上隅に表示されます。これまで私が持っているものは次のとおりです。uiimageview/uiscrollview(前後のズーム)でイメージを中央に配置する方法

UIImage *image = [UIImage imageNamed: @"t.bmp"]; 
self.view.backgroundColor = [UIColor blackColor]; 
self.mi.frame = CGRectMake(0, 0, image.size.width, image.size.height); 
self.mi.contentMode = (UIViewContentModeCenter); 
self.mi.backgroundColor = [UIColor blackColor]; 
[self.mi setImage:image]; 

/* Draw a line here!! 
UIGraphicsBeginImageContext(self.mi.frame.size); 
[self.mi.image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
CGContextSetLineWidth(context, 20.0); 
CGContextBeginPath(context); 
CGContextMoveToPoint(context, 0,0); 
CGContextAddLineToPoint(context, 200, 200);  
CGContextMoveToPoint(context, 200,0); 
CGContextAddLineToPoint(context, 0, 200); 
CGContextStrokePath(context); 
self.mi.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
*/ 

self.expimg.maximumZoomScale = 2.0; 
self.expimg.minimumZoomScale = 0.1; 
self.expimg.clipsToBounds = YES; 
self.expimg.delegate = self; 
[self.expimg setContentSize:self.mi.frame.size]; 

[self.expimg addSubview: self.mi]; 
[self.view addSubview: self.expimg]; 

ありがとう!

編集:miはUIImageViewで、expimgはUIScrollView(インタフェースビルダーを使用)です。また、viewForZoomingInScrollViewが定義されています。

答えて

1

イメージの位置とサイズを調整し、最初の読み込み時にズームレベルを変更することで、スクロール表示を行うことができます。 viewDidLoadで呼び出され、UIScrollViewDelegateのメソッドscrollViewDidZoom:で呼び出されるメソッドを作成します。 (ImageViewのとscrollViewは、クラス属性である)このような

何か作業をする必要があります:私は私の数学のいくつかが間違っている可能性が、これをテストしていません

- (void)centerImage { 

    CGSize screenSize = CGSizeMake(320, 480); 

    CGSize imageSize = imageView.frame.size; 
    imageSize.width = imageSize.width * scrollView.zoomScale; 
    imageSize.height = imageSize.height * scrollView.zoomScale; 
    CGSize scrollSize = scrollView.frame.size; 

    CGFloat centerX; 
    CGFloat centerY; 
    CGFloat left; 
    CGFloat top; 

    if (imageSize.width > screenSize.width) { 
     scrollSize.width = imageSize.width; 
     centerX = imageSize.width/2; 
     left = 0; 
    } else { 
     scrollSize.width = screenSize.width; 
     centerX = screenSize.width/2; 
     left = centerX - imageSize.width/2; 
    } 

    if (imageSize.height > screenSize.height) { 
     scrollSize.height = imageSize.height; 
     centerY = imageSize.height/2; 
     top = 0; 
    } else { 
     scrollSize.height = screenSize.height; 
     centerY = screenSize.width/2; 
     top = centerY - imageSize.height/2; 
    } 

    CGRect scrollFrame = scrollView.frame; 
    scrollFrame.size = scrollSize; 
    scrollView.frame = scrollFrame; 

    CGRect imageFrame = imageView.frame; 
    imageFrame.origin = CGPointMake(left, top); 

    scrollView.contentOffset = CGPointMake(centerX-(imageSize.width/2), centerY-(imageSize.height/2)); 

} 

。いずれにせよ、それはあなたに何をすべきかの良いアイデアを与えてくれるでしょう。

+0

はあなたのソリューションが動作しないズームが、私は答えを見つけながら:http://stackoverflow.com/questions/638299/uiscrollview-with-centered-uiimageview-like- photos-app btw、私のコメントでは、正しい方法で線を描くのですか?それはちょうど確かめたいと思っている – Alede

-1

[scrollView zoomToRect:CGRectMake(x、y、width、height)animated:YES];あなたのために働くでしょう... rectはこれらの中心座標を原点として持つべきです。

0

ステップ1:あなたはのviewDidLoadでそれにinitその後、画像表示プロパティを作成し、scrollViewに追加します。

self.pictureImage = [[UIImageView alloc]initWithImage:self.picture]; 
[_scrollView addSubview:_pictureImage]; 
self.pictureImage.alpha = 0; 
_scrollView.delegate = self; 

ステップ2:最小規模に

- (CGFloat)adjustZoomScale{ 
CGFloat scale = self.scrollView.bounds.size.width/self.pictureImage.bounds.size.width; 
CGFloat h = scale * self.pictureImage.bounds.size.height; 
if (h > self.scrollView.bounds.size.height) { 
    scale = self.scrollView.bounds.size.height/self.pictureImage.bounds.size.height; 
} 
return scale; 
} 
を取得するために adjustZoomScale関数を作成します

手順3:スクロールビューのフレームがviewDidAppearに更新されるので、ここでズームスケールを調整する必要があります。そして、画像を表示させるためにアルファを設定スムーズ:

- (void)viewDidAppear:(BOOL)animated { 
[super viewDidAppear: animated]; 
_scrollView.maximumZoomScale = 1.0; 
_scrollView.minimumZoomScale = [self adjustZoomScale]; 
[_scrollView setZoomScale:_scrollView.minimumZoomScale]; 
self.pictureImage.alpha = 1; 
} 

ステップ4:画像ビューをviewForZoomingInScrollView(スクロールデリゲートメソッド)を実装し、戻り

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ 
return self.pictureImage; 
} 

ステップ5:scrollViewDidZoom(スクロールデリゲートメソッドを実装)と画像ビューの調整ポイント

- (void)scrollViewDidZoom: (UIScrollView*) scrollView { 
CGSize boundsSize = scrollView.bounds.size; 
CGRect contentsFrame = _pictureImage.frame; 

if (contentsFrame.size.width < boundsSize.width) { 
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0; 
} else { 
    contentsFrame.origin.x = 0.0; 
} 

if (contentsFrame.size.height < boundsSize.height) { 
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0; 
} else { 
    contentsFrame.origin.y = 0.0; 
} 
_pictureImage.frame = contentsFrame; 
} 

ありがとう画像表示のための調整ポイントのthis postshawhu

関連する問題