Reputation: 6823
I create a view as follows:
// Create sub view for Logo
logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,screen.size.width,(screen.size.height - 230))];
[logoView setBackgroundColor:[UIColor blueColor]];
UIImageView *bgLogoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-welcomeLogoView.png"]];
[logoView addSubview:bgLogoView];
//NSLog(@"the button view height is %f",(screen.size.height - 230));
logoView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
I have a set box for the logoView. It is 230 in height (iphone4<) and 318(iphone5). I have created an image for this block which is 640x460px.
How can I make this image fit the view. At the moment it loads the fullsized image into the block which is actually half that size... The image was made double the size for quality. I am testing on an iphone4, running ios5.
I am sure there is a line or two missed to fit the image to the view bounds of the surrounding view in some way.
Upvotes: 0
Views: 6950
Reputation: 318824
You need to reset the image view's frame. As you have it, the image view's frame will be the size of the image. Also, set the image view's contentMode
. And lastly, set the image view's autoresizingMask
.
logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,screen.size.width,(screen.size.height - 230))];
[logoView setBackgroundColor:[UIColor blueColor]];
UIImageView *bgLogoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-welcomeLogoView.png"]];
bgLogoView.frame = logoView.bounds; // reset the frame
bgLogoView.contentMode = UIViewContentModeScaleAspectFit; // or other desired mode
bgLogoView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[logoView addSubview:bgLogoView];
//NSLog(@"the button view height is %f",(screen.size.height - 230));
logoView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
Upvotes: 3
Reputation: 76
Try this:
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 230)];
NSString *bgFilePath = [[NSBundle mainBundle] pathForResource:@"bg-welcomeLogoView"
ofType:@"png"];
UIImage *img = [[UIImage alloc] initWithContentsOfFile:bgFilePath];
[bgImageView setImage:img];
[logoView addSubview:bgImageView];
You can change the size of your UIImageView in initWithFrame:
Upvotes: 0
Reputation: 1376
Something like
bgLogoView.contentMode = UIViewContentModeScaleAspectFit;
Look at the contentMode property of UIView. The possible values are:
typedef enum {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
} UIViewContentMode;
Upvotes: 2