2011-07-28 18 views
5

imageview用のフレームを元の画像サイズに設定して、それをビューの中心に表示しようとしています。私はこれに次のコードを書いていますが、画像はまだ画面全体に表示されています!画像のサイズが320x88で、320x460にサイズ変更されていると思われます。画面の中央に表示したいのでY座標を(460-88)/ 2に設定しますが、動作しません。誰でもこの問題を解決するのを助けてください。ありがとう。iPhone:画像を元のサイズに戻す

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]]; 

if(imageView.image.size.width == IPHONE4_RES_WIDTH) 
    [imageView setFrame: CGRectMake(0, 0, imageView.image.size.width/2, imageView.image.size.height/2)]; 

UIViewController *viewController = [[[UIViewController alloc] init] autorelease]; 
viewController.view.backgroundColor = [UIColor blackColor]; 

NSLog(@"%f : %f",viewController.view.bounds.size.height, imageView.bounds.size.height); 

viewController.view = imageView; 
viewController.view.contentMode = UIViewContentModeCenter; 

[self.navigationController pushViewController:viewController animated:YES]; 

答えて

12

それはフルサイズが設定したときに、それが中央に画像が表示されます場合でも..あなたはuiimageviewを設定することができます

viewController.view.contentMode=UIViewContentModeCenter; 
+0

こんにちはラケッシュを、選択することができますが、それは何の違いもないようです!画像は画面中央に表示されますが、幅は640(iPhone4解像度)です!この問題に対処することはできますか?私の編集したコードを見てください。ありがとう。 – applefreak

+0

また、私のビューの背景色が黒く表示されません! – applefreak

+0

私は私の元の質問に基づいてあなたの答えを受け入れました。私はiPhone 4のサイズと背景色の問題を処理するコードを掲載しました!ありがとう。 – applefreak

2

これはイメージを伸ばすないようにしてみてください

imageView.contentMode = UIViewContentModeCenter 
+0

ありがとうございます。これも動作します!私はRakeshの答えに答えて言ったiPhone4のサイズの問題を解決することができません。それを取り除くことはできますか? – applefreak

0

以下は私の要件に完全に対応しています!

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]]; 

// Reduce the width and height for iPhone 4 res 
if(imageView.image.size.width == IPHONE4_RES_WIDTH) 
    [imageView setFrame: CGRectMake(0, 0, imageView.image.size.width/2, imageView.image.size.height/2)]; 


UIViewController *viewController = [[[UIViewController alloc] init] autorelease]; 
viewController.view.backgroundColor = [UIColor blackColor]; 

float X = (viewController.view.bounds.size.width - imageView.bounds.size.width)/2; 
float Y = (viewController.view.bounds.size.height - imageView.bounds.size.height)/2 - 50; // Size of tabbar and navigation bar deducted 

NSLog(@"(%f, %f) : (%f,%f)",X,Y,imageView.bounds.size.width,imageView.bounds.size.height); 

[viewController.view addSubview:imageView]; 

//viewController.view = imageView; 
//viewController.view.contentMode = UIViewContentModeCenter; 

// Set in the center of the screen 
if(imageView.image.size.width == IPHONE4_RES_WIDTH) 
    [imageView setFrame: CGRectMake(X, Y, imageView.image.size.width/2, imageView.image.size.height/2)]; 
else 
    [imageView setFrame: CGRectMake(X, Y, imageView.image.size.width, imageView.image.size.height)]; 

[self.navigationController pushViewController:viewController animated:YES]; 
2

storyboardファイルで同じことを達成しようとしていますが、ただ、添加物として:同じことのためにストーリーボードであなたが設定ImageViewののフレームを使用して動作しますが、画像はiPhone 4の解像度の幅であれば、私は半分の幅に必要Mode = CenterUIImageView

enter image description here

関連する問題