hanumanDev hanumanDev -4 years ago 91
iOS Question

CGRect image resizing

I'm using CGRect to display an image. I'd like the CGRect to use the width and height of the image without me specifying it.

can this:

CGRectMake(0.0f, 40.0f, 480.0f, 280.0f);


become this:

CGRectMake(0.0f, 40.0f, myImage.width, myImage.height);


some images get distorted when I specify the parameters.

here's the code:

CGRect myImageRect = CGRectMake(0.0f, 40.0f, 480.0f, 280.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:recipe.img]];


thanks for any help.

Answer Source

Once you have a UIImage, you can find its size by looking at the size property:

UIImage * image = [UIImage imageNamed:recipe.img];
CGRect rect = CGRectMake(0.0f, 40.0f, image.size.width, image.size.height);

UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];
[imageView setImage:image];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download