简体   繁体   中英

Disable UIImageView resizing behavior when contentMode UIViewContentModeScaleAspectFit

I have UIImageView with contentMode = UIViewContentModeScaleAspectFit

I need this mode to display my image properly. But also I need imageView cropping image when its frame changes like it was with UIViewContentModeTopLeft .

How I can obtain this?

Edited:

imageView.contentMode = UIViewContentModeTopLeft;
imageView.image = someImage;

Now I get this (and it's ok, image scaled properly and looks good):

在此处输入图片说明

Then, I have method:

- (void)setRating:(double)rating {
      double starWidth = imageView.image.size.width / _maxRating;
      CGRect oldFrame = self.frame;
      imageView.frame = CGRectMake(0, 0, rating * starWidth, oldFrame.size.height);
      _rating = rating;
}

after this method I need to get something like that:

在此处输入图片说明

BUT because of UIViewContentModeScaleAspectFit I get scaled image

You can't really get what you want from that imageView in the method you are attempting without a lot of extra work. When you resize the view you will see that scaling, that is the mode you set and thats really all that will happen. There are a number of possibilities, many involve resizing things yourself thru subclassing. But as for solutions:

  1. Size your art prior to runtime, and then you don't need to do the scaling for every display of those stars for every user and every run. Save the electrons!
  2. Add the imageView as a subview to some other container view which you will match to the scaled size of the imageView, then resize to clip the imageView portion you do not want displayed.

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