Why does a UIImageView get resized when assigning

2019-04-06 14:02发布

问题:

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

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

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

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:

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

I noticed this from the UIIMageView Documentation:

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.

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.

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

回答1:

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.

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

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