Fitting a UIImage to the center of screen

11,462

Solution 1

UIImage *backgroundLogoImage = [UIImage imageNamed:@"center_logo.png"];
UIImageView *backgroundLogoImageView = [[UIImageView alloc] init];
backgroundLogoImageView.frame = CGRectMake((self.view.frame.size.width / 2) - (backgroundLogoImage.size.width / 2), (self.view.frame.size.height / 2) - (backgroundLogoImage.size.height / 2), backgroundLogoImage.size.width, backgroundLogoImage.size.height);

Solution 2

There is a property "center": you can do something like this:

UIImageView * img = [[UIImageView alloc] init];
img.center = self.view.center;

Solution 3

Set your image View to be the size you want all the images to be. Sound to me like you want it to be full device screen.

  imageView.frame = self.view.frame; // or what ever;
  // if its a custom size,set size and then call imageView.center = self.view.center;

Add the image to the imageView but set content mode to allow scaling.

 UIImage *image = [UIImage imageNamed:@"something.png"];
 image.contentMode = UIViewContentModeScaleAspectFit;
 image.clipsToBounds = YES;
 imageView.image = image;

This should result in the image either scaling down or up to fit the imageView's frame.

Share:
11,462
Rashid Asgari
Author by

Rashid Asgari

Updated on June 04, 2022

Comments

  • Rashid Asgari
    Rashid Asgari almost 2 years

    I have a viewcontroller,in which a UIImageview is subviewed. The UIImages come in different size and orientations. But i wanna be able to fit them in the middle of the screen. I tried to define a frame for the UIImageview but i cant get the 4 parameters of the CGRectMake right.

  • mathema
    mathema over 7 years
    There is no contentMode or clipsToBound attributes in UIImage type. It's in the UIImageView
  • Khanh Le Tran
    Khanh Le Tran about 6 years
    Useful - But that works for UIImageView - not UIImage.