0

Problem:

Adding/removing subviews to/from a UIScrollView causes noticeable lag.

Details:

I have a UIScrollView (with paging enabled) that will contain a couple dozen 'pages'. The content size of this UIScrollview is the number of pages * the width of the UIScrollView.

Each of these pages is a subview that I build from some file system access. My UIScrollView keeps three pages around as subviews: the current page, and its left/right neighbours. Loading/eviction of the neighbouring subviews takes place when the current page is half offscreen, which is when the disk access and manipulation of view hierarchy occurs.

When I first encountered this my immediate thought was that the disk access/building the subview is the source of the lag. I've since moved to this an NSTimer, and am still experiencing the same lag.

I'm starting to suspect that the lag is caused by addSubview (adding subview to UIScrollView) and removeFromSuperview (removing subviews from UIScrollView).

Subviews are added directly to UIScrollView. I don't have a 'content view' inside the UIScrollView that I add them to.

Question:

Is this a common problem/bottleneck? Is there a better pattern for loading of these pages?

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
John Carter
  • 6,752
  • 2
  • 32
  • 53
  • 1
    if the subViews you are adding contain large resource files (images) then you may notice lag as the images are loaded into memory depending on the size of the images. – KDaker Jul 18 '12 at 01:55
  • Hmm. The subviews we're adding are a subclass of UIScrollView, and their content is an image that's 2048x2048, and usually weighs in about 2MB. – John Carter Jul 18 '12 at 02:06
  • 1
    try using small image files or no images and see if the lag is still there... if not, then you probably need to look into lazy loading the images. – KDaker Jul 18 '12 at 02:11

2 Answers2

1

The problem ended up being setting the image property of the subviews. UIImage does not perform its processing right away, only in preparation for appearing on screen -- this is what was causing the lag.

The question lead me down the right track:

Setting image property of UIImageView causes major lag

Their solution was to decompress/process the image ourselves using Core Graphics, and it worked beautifully.

Community
  • 1
  • 1
John Carter
  • 6,752
  • 2
  • 32
  • 53
0

NSTimer typically runs on the main thread. If you're doing synchronous disk access on the main thread, then that's going to cause stuttering. How are you reading your files?

Rob Napier
  • 286,113
  • 34
  • 456
  • 610