6

I am trying to set frame for imageview to original size of image and display it in center of view. I have written following code for this but image still appears in whole screen! Suppose image size is 320x88, it appears resized/stretched to 320x460! I want it to appear in center of screen so I set Y coordinate to (460-88)/2 but it doesn't work. Could anyone please help me to resolve this. Thanks.

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];
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139

4 Answers4

16

try this

    viewController.view.contentMode=UIViewContentModeCenter;
Rakesh Bhatt
  • 4,606
  • 3
  • 25
  • 38
  • Hi Rakesh, that works but if image is iPhone 4 resolution width, I need to half the width using setting imageview's frame but it doesn't seem to make any difference! Image appears in center of the screen but its width is 640(iPhone4 res)! Can I address this issue? See my edited code. Thanks. – Paresh Masani Jul 28 '11 at 09:36
  • Also I don't see my view's background color black! – Paresh Masani Jul 28 '11 at 09:38
  • I have accepted your answer based on my original question. I have posted the code that takes care of iPhone 4 size and background color issues! Thanks. – Paresh Masani Jul 29 '11 at 10:03
2

you can set the uiimageview to not stretch the image.. even if it is full size it will show the image in the center when you set

imageView.contentMode = UIViewContentModeCenter
Bastian
  • 10,403
  • 1
  • 31
  • 40
  • Thanks. This works too! but I am not able to resolve iPhone4 size issue that I mentioned in response to Rakesh's answer. Can I get rid of it? – Paresh Masani Jul 28 '11 at 09:37
1

Ran across this question while trying to achieve the same in a storyboard file. Just as an addition: in storyboard for the same thing you can select Mode = Center of UIImageView

enter image description here

Nikita Vlasenko
  • 4,004
  • 7
  • 47
  • 87
0

Following works perfectly for my requirements!

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];
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139