0
votes

I have an UIImageView 3 levels deep in a super view. (The white region on top of the gray rectangle is my superview, the gray elongated rectangle is one subview, the black square is another subview of the elongated gray rectangle, the cog is the image of the UIImageView which is a subview of the black square). The frame rectangle of the UIImageView is calculate as follows, where _normalImage is a UIImage object. I do this inside the subclass that represents the black square

    CGFloat xPoint = self.bounds.size.width/2 - _normalImage.size.width/2;
    CGFloat yPoint = self.bounds.size.height/2 - _normalImage.size.height/2;
    CGRect frameRect = CGRectMake(xPoint, yPoint, _normalImage.size.width, _normalImage.size.height);
    self.imageHolder = [[UIImageView alloc] initWithFrame:frameRect];

enter image description here

The _normalImage is 26X26 and should be a perfect square. However the image is rendering as distorted as if there is an aspect ratio loss.

Whats wrong ?

2
Is self in this context the view with the black background, and do you ultimately add imageHolder as a subview of the black view? - warrenm
Yes self is the black background. imageHolder is a subview of the black view - GarryO
So when you NSLog(@"%@", NSStringFromCGRect(self.imageHolder.bounds)), is the calculated frame the correct size? - warrenm
If you have a retina display, try doubling the image resolution and add "@2x" suffix to its name. - Abdullah Umer

2 Answers

0
votes

try using floorf() function for your coordinates to prevent them contain the fractional part.

0
votes

Image is stretched because by default it use UIViewContentModeScaleToFill content mode.

self.imageHolder.contentMode = UIViewContentModeScaleAspectFit;