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

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

问题描述

我正在尝试将图像设置为 UITableViewSimpleTableCellUIImageView.我已经更改了 imageview 的大小,但是每当我尝试设置图像时,它都会自动调整大小.我尝试使用代码 此处 来调整图像大小并进行设置.但它仅在我将图像尺寸设置为 (20x20) 时才有效,这是 UIImageView (40x40) 大小的一半.所以它出来模糊.我还尝试设置 UIAspectRatioFit/UIAspectratioFillclipsToBounds = 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天全站免登陆