Rashid Asgari
Rashid Asgari

Reputation: 75

Fitting a UIImage to the center of screen

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.

Upvotes: 5

Views: 12020

Answers (3)

Engnyl
Engnyl

Reputation: 1380

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

Upvotes: 6

BooRanger
BooRanger

Reputation: 1526

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.

Upvotes: 1

sher
sher

Reputation: 49

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

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

Upvotes: 3

Related Questions