proper fill an image larger than screen
Posted
by
madcat
on Stack Overflow
See other posts from Stack Overflow
or by madcat
Published on 2010-12-23T15:19:57Z
Indexed on
2010/12/24
7:54 UTC
Read the original article
Hit count: 223
iphone
what I wanted to achieve here is simply fit the image width to the screen on both orientations and use UIScrollView to just allow scroll vertically to see the whole image.
both viewController and view are created pragmatically.
the image loaded is larger than screen on both width and height.
here is the related code in my viewController:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)loadView {
UIScreen *screen = [UIScreen mainScreen];
CGRect rect = [screen applicationFrame];
self.view = [[UIView alloc] initWithFrame:rect];
self.view.contentMode = UIViewContentModeScaleAspectFill;
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIImage *img=[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"]];
UIImageView *imgView =[[UIImageView alloc] initWithImage:img];
[img release];
imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:imgView];
[imgView release];
}
tried all combinations for both contentMode above, did not give me correct result.
the most close I am getting now: I manually resize imgView in loadView, portrait mode would display correctly since app always starts with portrait mode, but in landscape mode, the width fits correctly, but image is centered vertically rather than top aligned.
if I add the imgView to a scrollView, in landscape mode it looks like contentSize is not set to full image size. but when I scroll bounce I can see the image is there in full size.
question:
why I need to resize it manually?
in landscape mode how and where I can 'move' the imgView, so imgView.frame.origin is (0,0) and works correctly with a scroll view?
Thanks!
UPDATE: I added: imgView.clipsToBounds = YES;
and find out in landscape mode the image bounds is smaller than screen in height.
so the question becomes how to have the image view keeps original ratio (thus shows the full image always) when rotated to landscape? do I need to manually resize it after rotation again?
© Stack Overflow or respective owner