简体   繁体   中英

UIImageView is ignoring my ContentMode setting

I've found some other answers here, but they don't seem to apply/work, so I figure it might be due to my particular code.

In my app, I have an array of UIImages, which are actually scaled down photos taken by the iPhone camera.

I am attempting to show the UIImage within a scrollView, correctly scaled to show the entire image.

This is my code:

scrollView.pagingEnabled = YES;
scrollView.clipsToBounds = NO;

CGFloat contentOffset = 0.0f;

pageControl.numberOfPages = [imageArray count];
pageControl.currentPage = 0;

for(UIImage *image in imageArray)
{
    NSLog(@"image width = %f, image height = %f", image.size.width, image.size.height);

    CGRect imageViewFrame = CGRectMake(contentOffset, 0, scrollView.frame.size.width,                 scrollView.frame.size.height);

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
    [imageView setImage:image];
    [imageView setClipsToBounds:YES];
    [imageView setContentMode:UIViewContentModeScaleAspectFill];  //I've tried others, but nothing seems to show the entire image
    [scrollView addSubview:imageView];
    [imageView release];
    contentOffset += scrollView.frame.size.width;
    [scrollView setContentSize:CGSizeMake(contentOffset, 0)];
}

Just to note, this is a portrait-oriented app and the image itself was taken in portrait orientation.

The NSLog output:

image width = 387.200012, image height = 518.400024

The image that I see show up on the UIImageView (within the scrollview) is a zoomed up image of the center of the image, no matter what I change the contentmode to.

What am I doing wrong here? thank you!

The answer is what I had commented earlier: the scaling factor of the UIImage caused the ContentMode not to work. Or at least, made it seem not to work.

I know this has been asked and answered but it seems there is another cause for this issue lately. As of tvOS 9.1 , setting adjustsImageWhenAncestorFocused seems to render contentMode obsolete (a possible bug) for UIImageView . I hope this helps someone.

You set a content size for the scrollView with a height of 0 and the contentOffset as width ... that obviously can't work.

You have to set the content size of the scroll view to the size your image has. That is the meaning of the content size - if it is bigger than the scroll view's size, the scroll view will let you scroll to see the parts that are not on the screen.

What you apparently want to do is to set the frame of the imageView to a contentOffset of n times the width of the screen so you can scroll horizontally through the images. So, set the size of the UIImageView to your screen size, and it's origin to (n * screenSize.width, 0), and the scroll view's content size to ((n+1)*screenSize.width, screenSize.height), with n being the image count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM