Question

I'm building an app for the ipad that uses the youtube api to get the video thumbnails and present them in a gallery (like the native youtube app on the ipad). Basically it's just a bunch of UIImageViews on a UIScrollView. Now the problem is that displaying all the images at once may cause the app to crash due to memory problems, so the idea is to keep only the images which can bee seen, and write back the others to files.

My problem is how to get the exact "visible" area bounds of the content of the UIScrollView.

Any ideas?

Was it helpful?

Solution

Have you taken a look at AQGridView? It's designed for precisely this sort of thing.

OTHER TIPS

Watch WWDC 2010 Session 104 - Designing Apps with Scroll Views.

When UIScrollView scrolled you should check the content offset (scrollView.contentOffset) and get (for example) the x value. With that (and depending on the width frame value of your uiscrollview), you should be able to know what the user is seing. Try printing these informations with a NSLog ;-)

And then when you know which "page" the user is seing, you could load some UIImageView.

Good Luck :-)

In layoutSubviews you can get the currently visible bounds like this:

CGRect visibleBounds = [self bounds];

You can then check if a UIView (e.g. UIImageView) is visible by comparing its frame with visibleBounds:

BOOL visible = CGRectIntersectsRect(visibleBounds,cellFrame);

Simply check each visible view and remove those that are outside of the visible bounds. Then check the dataset for images that are visible, but don't have a view yet.

You should also reuse UIViews instead of creating new ones all the time. Simply queue removed ones in an NSSet and check that set before creating new ones.

Here's a different approach:

Use a UITableView but adjust the properties so it looks like a regular scroll view. (Basically, include several thumbnails on each row, and remove the row separator.)

As rows move off the screen, the views will be recycled and the memory used by your UIImageViews can be reclaimed.

Note that this requires some extra work to know exactly which thumbnail was pressed by the user, but it's not too hard. I've done this in the past and used the tag property to store an array index. It's not ideal, but it's a simple enough hack.

Note that this only works if you're table only scrolls vertically. As far as I know, you can't have a horizontally scrolling UITableView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top