iPhone: Display image at its original size

applefreak picture applefreak · Jul 28, 2011 · Viewed 8.7k times · Source

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

Answer

Rakesh Bhatt picture Rakesh Bhatt · Jul 28, 2011

try this

    viewController.view.contentMode=UIViewContentModeCenter;