Search code examples
iosobjective-cuitableviewcgaffinetransform

UITableViewCell Transform Breaks in iOS 11


In an app I've built, I had a table view showing an image to the left of each cell by running the following code inside of cellForRowAtIndexPath:

cell.imageView.image = [UIImage imageNamed:myImage];

But the image was too big, so I shrunk it:

cell.imageView.transform = CGAffineTransformMakeScale(0.3, 0.3);

This worked just fine in iOS 10. But once I upgraded to the newest Xcode with the iOS 11 SDK, the images got enormous. It turns out that that second line of code transforming the image view is now doing nothing: I can comment it out, change the 0.3's to something else, etc., and it doesn't make any difference. CGAffineTransformMakeScale still has documentation in the new Xcode, so I'm assuming it wasn't deprecated, but then why did this break with iOS 11, and how do I fix it? Any ideas? Thanks in advance! Please note, I'm using Objective-C.

Edit:

Just tried 3 changes to the code:

  1. Change the second line to cell.imageView.transform = CGAffineTransformMakeScale(0.0000001, 0.0000001);. Nothing happens (i.e., the image views and the images inside them are still just as huge).

  2. Change the second line to cell.imageView.transform = CGAffineTransformMakeScale(0, 0);. The image disappears from the image view, but the image view is still the same size, and you can tell because it still displaces the text in the cell and pushes it far to the right.

  3. Remove the first line of code (no longer assigning an image to the imageview). The imageview disappears, and the text moves all the way to the left of the cell.

Perhaps this can help shed some light on what's going on?


Solution

  • Found an answer to my own question, with credit due to Paul's answer from this question: How to resize a cell.imageView in a TableView and apply tintColor in Swift

    CGSize  itemSize = CGSizeMake(50, 50);
    UIGraphicsBeginImageContextWithOptions(itemSize, false, 0);
    CGRect  imageRect = CGRectMake(0, 0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    I still don't know why the old CGAffineTransformMakeScale doesn't work anymore, but this gets the job done.