UIImageView自动调整大小 [英] UIImageView resizes automatically

查看:86
本文介绍了UIImageView自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在中将图像设置为 SimpleTableCell UIImageView 的UITableView 。我已经改变了imageview的大小,但每当我尝试设置图像时,它会自动调整大小。我尝试使用代码此处来调整图像大小并进行设置。但它仅在我将图像尺寸设置为(20x20)时才有效,该尺寸是 UIImageView (40x40)尺寸的一半。所以它出来模糊了。我还尝试设置 UIAspectRatioFit / UIAspectratioFill clipsToBounds = YES 。似乎没什么用。

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.

另一个奇怪的部分是当我使用图像时, imageView 不会自行调整大小直接从网上下载如下:

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?

推荐答案

你的SimpleTableCell是你的 UITableViewCell 的子类产生的?这个imageView是这个子类的属性吗?

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

只是一个猜测:

每个UITableViewCell都有一个内置的imageView只读属性。在你发布的代码中,你正在使用这个默认属性,我认为它不能修改它的框架,它在单元格的左侧是固定的。

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.

在文档中查找imageView属性: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/index.html#// apple_ref / occ / instp / UITableViewCell / imageView

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

因此,如果你的自定义单元格有自己的imageView,请确保不要调用它 imageView 也是因为你可能会使用默认的 UITableViewCell 的属性,而不是你自己的属性,它将拥有你的所有框架自定义等等。

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.

您应该通过customClass引用您的单元格并像这样调用您的属性:

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.

这篇关于UIImageView自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆