iPhone: Display image at its original size
Asked Answered
S

4

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];
Seta answered 28/7, 2011 at 9:7 Comment(0)
P
16

try this

    viewController.view.contentMode=UIViewContentModeCenter;
Province answered 28/7, 2011 at 9:15 Comment(3)
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.Seta
Also I don't see my view's background color black!Seta
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.Seta
P
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
Pointdevice answered 28/7, 2011 at 9:19 Comment(1)
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?Seta
N
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

Nikolenikoletta answered 30/6, 2016 at 23:4 Comment(0)
S
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];
Seta answered 29/7, 2011 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.