开发者

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.

开发者_JS百科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];


try this

    viewController.view.contentMode=UIViewContentModeCenter;


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


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

iPhone: Display image at its original size


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜