Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

ios - UIImageView resizes automatically

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

Another strange part is that the imageView doesn't resize itself when I use an image directly downloaded from the web like so:

- (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 store this image into a documents directory and then try to reload in elsewhere, the resizing occurs. Any solutions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your SimpleTableCell is a subClass of UITableViewCell that you created? This imageView is a property of this subclass?

Just a guess:

Every UITableViewCell has a built-in imageView read-only property. In the code that you posted, you are using this default property, which I believe cannot have it's frame modified, it's kind of fixed in the left side of the cell.

Look for 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 do have your custom cell with your own imageView, make sure to not call it imageView too, because you may end using the default UITableViewCell's property, and not your own, which will have all your frame customizations and etc.

You should reference your cell by your customClass and call your property like this:

- (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;
}

Don't know if the problem is really related do this, so let me know if it helped or not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...