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?
If you are resizing UIImageView manually, set the content mode to UIViewContentModeScaleAspectFill . If you want to keep content mode UIViewContentModeScaleAspectFit do not resize imageview to 313. Image will adjust maximum possible width and height , keeping it's aspect ratio.
To get the native size of the image just select the image and press Command + = on the keyboard. the to re-size it proportionally select the corner and hold down the shift key when you re-size it.
Open the Library, look for "Tap Gesture Recognizer" object. Drag the object to your storyboard, and set the delegate to the image you want to trigger actions. Then go to the view controller, drag the same object to set the IBAction.
An object that manages image data in your app.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With