在iOS 6中分配新图像时,为什么UIImageView会调整大小? [英] Why does a UIImageView get resized when assigning a new image in iOS 6?

查看:55
本文介绍了在iOS 6中分配新图像时,为什么UIImageView会调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序包含一个包含自定义UITableViewCell的UITableView.该单元格又​​包含一个UIImageView.

An application contains a UITableView containing a custom UITableViewCell. The cell in turn contains a UIImageView.

问题在于,在cellForRowAtIndexPath中设置图像会使图像占据整个UITableViewCell区域:

The problem is that setting the image in cellForRowAtIndexPath makes the image take up the entire UITableViewCell area:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bigrect" ofType:@"png"];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];

    cell.imageView.image = image;

    return cell;
}

在IB中,已选择纵横比拟合"作为模式,但是更改此字段对结果没有明显影响.

In IB, 'Aspect Fit' has been selected as the mode, but changing this field has no apparent effect on the result.

但是,当从IB设置图像时,在我的代码中未调用cell.imageView.image = image时,结果正是我想要看到的.图像保持在我为IB中的UIImageView定义的范围内,并且不会尝试缩放以适合UITableViewCell的整个垂直高度:

However, when the image is set from IB, with no call to cell.imageView.image = image in my code, the result is exactly what I'd like to see. The image stays within the bounds I've defined for the UIImageView in IB and does not attempt to scale to fit the entire vertical height of the UITableViewCell:

如果重要的话,我正在使用的图像为1307x309像素.测试是在iOS 6.1模拟器上运行的.

The image I'm using is 1307x309 pixels, in case that's important. Tests were run on iOS 6.1 simulator.

我从 UIIMageView文档中注意到了这一点:

在iOS 6及更高版本中,如果您为该视图的restoreIdentifier属性分配一个值,它将尝试保留所显示图像的框架.具体来说,该类保留视图的边界,中心和变换属性的值以及基础层的anchorPoint属性.在还原过程中,映像视图将还原这些值,以使映像完全像以前一样显示.有关状态保存和还原如何工作的更多信息,请参阅《 iOS App编程指南》.

In iOS 6 and later, if you assign a value to this view’s restorationIdentifier property, it attempts to preserve the frame of the displayed image. Specifically, the class preserves the values of the bounds, center, and transform properties of the view and the anchorPoint property of the underlying layer. During restoration, the image view restores these values so that the image appears exactly as before. For more information about how state preservation and restoration works, see iOS App Programming Guide.

但是,我在这里或文档中的其他地方都找不到解决问题的方法.将"Foo"的恢复ID"添加到IB中身份"下的UIImageView不会更改行为.取消选中使用自动布局"也不会更改行为.

However, nothing here or elsewhere in the documentation I could find solved the problem. Adding a "Restoration ID" of "Foo" to the UIImageView in IB under 'Identity' did not change the behavior. Unchecking 'Use Autolayout' also did not change the behavior.

设置图像时如何防止iOS重新调整UITableViewCell中UIImageView的大小?

How can I prevent iOS from resizing the UIImageView in a UITableViewCell when setting the image?

推荐答案

事实证明,UITableViewCell显然已经具有一个名为"imageView"的属性,该属性覆盖了单元格的整个背景.设置此imageView对象的image属性会设置背景图片,而不是我感兴趣的图片.

It turns out that UITableViewCell apparently already has a property called "imageView" that covers the entire background of the cell. Setting the image property of this imageView object sets the background image, not the image I was interested in.

在确保CustomCell具有"myImageView"属性的同时将我的方法更改为以下内容:

Changing my method to the following while ensuring that CustomCell has a "myImageView" property fixed the problem:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bigrect" ofType:@"png"];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];

    cell.myImageView.image = image;

    return cell;
}

这个SO答案对于一个稍有不同的问题向我指出了正确的方向.

This SO answer to a slightly different question pointed me in the right direction.

这篇关于在iOS 6中分配新图像时,为什么UIImageView会调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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