Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Display image at its original size

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];
like image 870
AlienMonkeyCoder Avatar asked Jul 28 '11 09:07

AlienMonkeyCoder


2 Answers

try this

    viewController.view.contentMode=UIViewContentModeCenter;
like image 126
Rakesh Bhatt Avatar answered Oct 22 '22 11:10

Rakesh Bhatt


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
like image 25
Bastian Avatar answered Oct 22 '22 11:10

Bastian