2

I am subclassing a UITableViewCell and in the drawRect: method I want to draw a stretchable image but I have experienced that this has a big impact on the scroll performance.

Does anyone know how I can use a stretchable image and still have a good scroll performance? Alternatively, I could make five images and draw those instead. One for each corner and a center image which would be stretched. This just seems such a hassle, and I actually thought that's what a stretchable image would do.

EDIT: If I should split up the image and stretch it myself, it should of course be nine images, not four. One for each corner, one for top middle, bottom middle, left middle, right middle and one for the center.

simonbs
  • 7,932
  • 13
  • 69
  • 115

1 Answers1

2

Try using [UIImage resizableImageWithCapInsets:] to create a resizable image instead of your own code. Create an UIImageView with image and just add it as subview to cell.contentView at index 0

NSIntegerMax
  • 542
  • 2
  • 5
  • This gave me a significant better performance! Though, adding it as a subview isn't a great idea. Use `drawInRect:` or `drawAtPoint:` on the UIImage instead. – simonbs Jan 22 '12 at 18:59
  • 1
    I've noticed that resizableImageWithCapInsets: chooses different techniques depending on the gap width between the insets. If it is a 1pt gap then it uses UIImageResizingModeStretch which has way better scrolling performance. If the gap is bigger it uses UIImageResizingModeTile which sucks for scrolling but is great for resizing the UIImageView (vice versa for Stretch). In iOS6 you can select these modes manually via resizableImageWithCapInsets:resizingMode:. – orj Nov 21 '12 at 00:52
  • @orj, thank you very much. I was lucky to have found this piece of information here and fixed a problem that got me stuck for a couple of weeks already. You saved my day! – John Nov 03 '13 at 19:47