简体   繁体   中英

Adjusting UIImageView in UIScrollView of custom UICollectionViewCell

I am using custom UICollectionViewCell, the cell contains one UIImageView of size (320,145), but I need to display image of 400 x 400 into that, so I am trying to use UIScrollView to display whole image without cropping it, ie user can scroll to see whole image in parts.

I added my imageView in scrollView with giving proper frame size and content size to scrollView as given in following code. But still image is not looking properly, I can see the image is becomes small to fit the screen, I can see the whole image and scroll also, but I want to see full image ie 400 x 400.

UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 145)];
[imgScrollView setScrollEnabled:YES];
[imgScrollView setClipsToBounds:YES];
[imgScrollView addSubview:_albumImageView];
[imgScrollView setBackgroundColor:[UIColor yellowColor]];

[imgScrollView setContentSize:CGSizeMake(400, 400)];  /// my _albumImageView is of 400 x 400 

_albumImageView.contentMode = UIViewContentModeScaleToFill;
_albumImageView.clipsToBounds = YES;

[self.contentView addSubview:imgScrollView];

The output of above code is looking as below Screenshot 在此处输入图片说明

but I want it as below screenshot with scroll enable.(the image may be different with same size) 在此处输入图片说明

I think it's because you've set the contentMode property to UIViewContentModeScaleToFill

_albumImageView.contentMode = UIViewContentModeScaleToFill;

which will cause the image to resize to fill the frame (320x145).


EDIT:

Also, is the image in your _albumImageView a resizable UIImage? If not, make it resizable with

UIImage *img = [albumImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];

before adding it to your _albumImageView

Try this

_albumImageView.contentMode = UIViewContentModeScaleAspectFill;

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