Lazy loading of subViews into a non-paging UIScrollView - iphone

I am trying to implement a filmstrip-like UIScrollView that will be populated with thumbnails of catalog pages. Selecting a thumbnail image will cause the main UIScrollView to move to the selected page. The Catalog may contain 100 - 200 pages, and I want to load them lazily only when required.
I have done this in a UIScrollView with paging enabled, but haven't seen anything on the best way to do this in a non-paging scenario. There will be 6 thumbnails visible in the UIScrollView (+ 1 when the view is being scrolled) at any one time. I want to dequeue and reuse the thumbnail's UIView when the view is scrolled, as I am doing in the main UIScrollView (which is a paging scroll view).
Thanks -
Jk

I am also going to suggest you take a look at some sample code of Apple, that is, Photo Scroller. If you are a registered iOS developer, you should also take a look at the WWDC10 session about scroll views in iPhone applications.
http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Listings/main_m.html
What you need to do is mimic the behavior of a table view (which is nothing more than a subclass of UIScrollView). What you should mimic is the reuse of the cells. It is pretty easy to implement and will dramatically reduce the memory foot print of your application since you only load the content that is currently visible in the scroll view.
I hope this helps.

Check out the scrollview suite sample code from apple. The tiled example can probably be repurposed very easily.
http://developer.apple.com/library/ios/#samplecode/ScrollViewSuite/

Check out this class..it may proove helpful..
VSScroller

Related

UIScrollViews and Dynamically Creating Pages

I want to be able to dynamically populate UIScrollView. Like how it is done for row views in UITableView. I have a class that takes in some parameters and creates the respective view when it scrolls. Currently I have 8 views.
These 8 views have different background image, image and label according to the page number it is currently in. However the basic skeleton for this view is the same. What i am doing right now is in ViewDidLoad I am creating 8 views and add them as subviews and scroll over.
I don't want to do this. I want to create three views and the rest i want to populate when the user scrolls a page and then a page etc. How do i do this? ANy pointers/tutorial?
First of all you calculate the ContentSize for the scrollview (when you want to use the iPad in landscapemode with 8 pages then the width should be 1024*8 = 8192px and the height 768px).
Then you should implement the UIScrollView Delegate method:
scrollViewDidEndDecelerating:
In this Method you check on which page you currently are with the contentOffset property of the scrollview and start updating your left and right hidden views..
Hope this helps you a bit.
Check the two most recent WWDC videos for two excellent sessions regarding expert use of UIScrollViews. Additionally you can review a brief tutorial here, written by the well known cocoa expert, Matt Gallagher.

How to increase the scrolling performance in my table view with images in iphone?

I am new to iPhone development. I am parsing a xml and display the title, date, contents and image in the cell. Now the scrolling is not smooth, it is stuck.
How can I increase it? I have already applied lazy loading in another view, I am not able to apply in the new view. So how can I increase the scrolling performance? Can I check for any condition that if image is already loaded in the image view, so I can stop once again the loading of image view?
What I do to guarantee fast scrolling in a table is to subclass my own UITableViewCell where I implement all properties that I need.
Whenever that tablecell is initialized I draw the properties of the cell on the cell itself. So when you need an image on there, dont use an UIImageView, but just draw the image on the cell. Also all the text I need I just draw on there.
After a long stuggle finding out how to do all this, I also found a nice blog post about this.
http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/
First of all, check if you're not resizing the images - that takes a lot of computing power, and will slow down the table for sure.
Second of all, check for view hierarchy - complicated view hierarchy means bad performance. Rembemer to use as much opaque views as possible, and when you're using non-opaque views don't make the cells too complex(by complex Apple means three or more custom views). If the views are too complex - use drawRect method for custom drawing of the content.
There's a great example on how to achieve this provided by Apple called AdvancedTableViewCell (here's a link), and there's a great tutorial by Apple about table view cells (another link).

Mini Scrollbar with thumbnails on iphone

I was wondering, what would be the best way, to implement an 'extra' scrollbar, showing thumbnail versions of the pages, you are currently viewing.
In my case, I would like to have maybe around 20 images, which I would animate with Cover Flow Layers and while I 'scroll' up an down to view them, a scrollbar appears at the side, showing the smaller thumbnail versions of all the pages while the thumbnail currently visible is rendered slightly highlighted.
Is there anything specific I need to keep in mind? Thanks in advance for any ideas!
You are not giving much info to work with:)
Build a UIScrollView and a UIView, stick all the thumbnails into the UIView, stick the UIView into the UIScrollView.
If you have a lot of images, consider loading them in as "dummies/blank image" and have an NSOperation load them in the background. Consider releasing images that are outside of the screen, so that if 3-7 is on the screen then you only load in 2-8 and release everything else.
Regarding the logistics of it:
Build a viewController for holding everything. Then build a thumbnail viewController "component" that has a delegate method for setting the "displayed" image and maybe one for scroll and click.
Build a FullSize viewController component for handling the full size images. Have that also implement some delegate methods to communicate to the top most viewController. Add the two components to the top viewController, hook up the logic and you are done.
Make sure there exists only one model array/dictionary in the top most viewController and "feed" that downwards to the thumbnail and fullsize viewController, when some one clicks or scrolls, inform the top most viewController and have that update the other viewController.
Was that the kind of answers you were looking for?
:)

UIScrollView vs. UITableView

For a vertical scroller are there any advantages to using UIScrollView over UITableView?
I ask because I am currently using two vertical UIScrollViews with UIImageViews inside of them and am having memory issues and poor scrolling performance. I am not doing much with the scrollers, only highlighting images as they scroll into the center of the scrollviews and adding a delete button above an image if the user wants to remove it. I've started to look at lazy image loading/reuse and it seems that most of these issues have already been resolved in the UITableView class, so I'm wondering if there's any reason to stick with UIScrollView?
You should be able to use a UIScrolLView with no problem if you just, like you said, lazy load the controllers, when scrolling (assuming your views are full size) you dont need to have more than 3 views loaded at any one time, all you need is the current view, the view that goes behind it and infront of it, as you scroll your view you can unload and load the appropriate views. You should look into Page Control sample application, it does exactly this and you can pretty much get all the lazy loading code from there. Link is here https://developer.apple.com/iphone/library/samplecode/PageControl/index.html

Adding UiImageView to UiScrollView one at a time... but how?

My problem is that my 300 images are being added to UiScrollView and shows up after it's done finishing with addsubview: calls.
How do I set it up so that it adds a subview, who's up on the Screen .. and continue adding ?
You could add to UIScrollView only first couple of pictures that fit the screen and then add others on request, when user scrolls you have to check bounds and add new image if it would be displayed.
I'm not sure about the code details though.
I know for a sure that UIScrollView with 3 images are shown in demo in the lecture 8 (including source code) at Stanford's CS193P course about iPhone development, the presenter (a guy from Apple) mentioned in the video (on iTunesU) that you could load up on demand other images - he just gave general pointers not the actual solution.
But if you need to display screen full of icon size images (kind of like iphone image library) maybe it would be better to try UITableView (I haven't used it for that purpose yet but give it a try).
stefanB's answer is a good one. For the code details, I'd suggest you take a look at - [UIView drawRect:]. Given the size of each of your rows, you can easily calculate when the passed rect represents a new cell being brought into view due to a scroll, and then update with the new image accordingly.
Have you thought about putting your images not in a UIScrollView, but in a UITableView and having that handle all the caching and lazy loading for you? It sounds more specialized to handle what you'd like to do.