UIImageView自动调整大小

Ram*_*ain 3 uitableview uiimageview uiimage ios

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

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

但随后我将此图像存储到文档目录中,然后尝试在其他地方重新加载,进行大小调整.有解决方案吗

Luc*_*rdo 14

你的SimpleTableCell是UITableViewCell你创建的子类吗?这个imageView是这个子类的属性?

只是一个猜测:

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

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

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

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

不知道问题是否真的与此有关,所以让我知道它是否有帮助.

  • 是的,尽管我一直在努力避免这样做,但最终还是这样做了。但是,谢谢您的回答。:) (2认同)
  • 你的回答救了我的命 (2认同)