1

Guys I'm having some troubles subclassing an UIView. I'm creating an IconView. Simply it's a container for some other subviews. In my IconView i have this iVar: UIImageView _background UIImageView _icon UILabel _iconLabel.

When I initialize the IconView I setup this 3 iVar with images, text and some quartz effect like roundCorner and Shadow and then I add them to the self view. Everything is Ok but if I insert some of this IconView (i.e. 10) inside an empty scrollview the scroll effect is not smooth. I tried before inserting thousand of simple UIViews in a scrollview and the scroll animation works perfectly. With just 10 of my IconView the scroll animation works really bad.

I could approach differently retaining UIImages instead of UIImageViews and draw it inside drawRect: method but in this case I'm gonna loose Autoresizing property and Quartz effect.

Any suggests? Thank, Gabriele.

STW
  • 44,917
  • 17
  • 105
  • 161
Gabriele
  • 1,163
  • 1
  • 11
  • 24

2 Answers2

1

Unfortunately, a UIScrollView gets slow pretty fast. There are a lots of posts and articles on this topic out there, like this Question and this (defect) blogpost along with it's sample code. There are also three sessions about 'Performance optimization in iOS' in the 2010's WWDC videos which I highly recommend to watch. To summarize the conclusions: Use as few subviews as you can and take special care of avoiding transparencies.

Ok, so much for the general 'Performance in ScrollViews' talk, now to your case: Having the same problem, I used all the tips from the articles and videos above and while they improved the performance, it just wasn't enough. I had, like you, used rounded corners one some images and I found out that this absolutely kills performance. Just deactivating them helped more than everything else. It's probably the same with the shadow effects.

Now, most likely, you want to keep those rounded corners. I would suggest that you create a copy of your images (or take the original, if possible) and than manipulate them directly, using those awesome classes. This way, the effects will only be applied once. It works perfectly for me. For you shadows, you can probably just create some in Photoshop and use them in a new ImageView.

If that isn't enough, you should try to cache your IconViews, like TableViewCells are cached, if you don't already do this.

Community
  • 1
  • 1
Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
  • I tried to remove all Quartz effect and it helped a lot. Thanks but, Means that Quartz is pretty unusable? Should I use it only for static render? I don't think so ... I'll read all the articles and I'll watch the videos to improve performance. – Gabriele Oct 23 '11 at 17:19
  • Quartz just doesn't do well in scrollviews. If deactivating it solved your problem, the videos and articles won't, at least not that well. If shouldRasterize doesn't help, you really should go for the direct manipulations, as I suggested. – Philipp Schlösser Oct 23 '11 at 17:24
  • I started reading WWDC 10 lecture about Performance Optimization but I need to find time to watch the videos. In this moment I disabled all Quartz effect and I rounded corner with Categories suggested. Awesome!! Now the scroll with hundred of IconView is really fast but with thousand start to go slowly but in my case I need to use jus 15 or 20 of this inside my scroll. The conclusion for the moment is, as you said, do not use Quartz inside scroll :) – Gabriele Oct 24 '11 at 21:50
1

The problem will probably be the Quartz shadows. They can really slow the rendering down if used a lot.

Before you write them off, you might try setting your CALayer's shouldRasterize property to YES. This makes quartz render the shadow only once and store it in a buffer. See how it goes.

joerick
  • 16,078
  • 4
  • 53
  • 57
  • You should also set `rasterizationScale` to the screen scale. It's accessible through `[[UIScreen mainScreen] scale]`, but I believe there are some complications in API differences... see http://stackoverflow.com/questions/3130420/how-to-call-uiscreen-mainscreen-scale-in-a-universal-app-for-the-iphone-and – joerick Oct 23 '11 at 17:11
  • shouldRasterize in helps, it is introduced in one of the videos I mentioned. If you don't need any animations within your cells, you can give it a shot. – Philipp Schlösser Oct 23 '11 at 17:11
  • @joerick it helped. Now the animation is good but isn't smooth. – Gabriele Oct 23 '11 at 17:21