UIImageView automatically resizes

I am trying to set a UIImageView image in SimpleTableCell in a UITableView . I resized the image, but whenever I try to set the image, it automatically resizes. I tried using the code here to resize the image and set it. But it only works when I set the image size (20x20), which is half the size of the UIImageView (40x40). So it comes out blurry. I also tried setting UIAspectRatioFit/UIAspectratioFill and clipsToBounds = YES . Nothing seems to work.

Another strange part is that imageView does not resize when I use an image directly downloaded from the Internet, for example:

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { UIImage *userImage = [UIImage imageWithData:userImageData scale:self.profileImage.frame.size.height/self.profileImage.frame.size.width]; UITableViewCell *cell = [self.myTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; cell.imageView.image = userImage; } 

But then I save this image in the document directory, and then try to reload it in another place, there is a resizing. Any solutions?

+3
source share
2 answers

Is your SimpleTableCell a subclass of the UITableViewCell you created? Is this imageView a property of this subclass?

Just to guess:

Each UITableViewCell has a built-in readView property for reading. In the code you posted, you use this property by default, which, I believe, cannot have its frame, it is fixed on the left side of the cell.

Look at the imageView property inside the documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/index.html#//apple_ref/occ/instp/UITableViewCell/imageView

So, if you have a custom cell with its own ImageView, don't forget to also name it imageView , because you can end using the default UITableViewCell property, not your own, which will have all your frame settings, etc.

You must reference your cell with your customClass property and call your property as follows:

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { UIImage *userImage = [UIImage imageWithData:userImageData scale:self.profileImage.frame.size.height/self.profileImage.frame.size.width]; SimpleTableCell *cell = (SimpleTableCell*)[self.myTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; cell.myImageView.image = userImage; } 

I don't know if the problem is really related to this, so let me know if this helped or not.

+14
source

When a strange resize happens for me, Autolayout is my first suspect. Try disabling the self-timer and see if resizing still takes place.

  • Choose ViewController in storyboard or NIB
  • Click the File Inspector tab on the right side of your Xcode window
  • In the "Interface Designer Document" section, clear the "Use autorun" checkbox

If you need Autolayout, you probably have to fight it with some code.

+1
source

Source: https://habr.com/ru/post/974220/


All Articles