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

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.

Related

UIImageView in UIScrollView

I have a series of UIImageViews in a UIScrollView. The user can zoom into each one, but after a bit of usage the images start disappearing, and then various images in the app disappear. I'm wondering if anybody has any any experience with this and has any idea why this could be happening?
Its a complete guess until you give us source, but chances are that you are doing something like not retaining your image views or are adding them to another view – effectively removing them from the first view they were in.

UIScrollView question... loading images

I have a UIScrollView that does not do paging. I am loading about 500 small (64x64-iPhone 192x192-iPad) images. I do not want to load them all into memory. I would like to load them as the user scrolls.
Can anyone tell me where I can set the images to display? I have an object for each image that holds the image resource name, index number and x and y coordinate where it should be displayed in the UIScrollView. I was hoping there was a method I could override where I could create a UIImageView and then add the view to the scroll view on the fly... maybe preloading 6 to 10 images at a time. Every sample I see uses paging and only displays one image at a time.
Any help or example is greatly appreciated.
Thank you
EDIT:
In the ScrollViewSuite examples from Apple there is a Tiling example. That example has a scrollview that popups up displays images to select from. That is exactly what I am trying to do, but Ineed to load them on the fly. The sample loads all of the thumbnail images into memory. Someone somewhere must be doing this because the iPod/iPhone and iPad do not have alot of memory to load all the images.
Apple has some really good demo code that shows how to do this. Check out TiledScrollView.m especially the layoutSubviews method.
write the code in following method of scrollview:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
I did the same. Load atleast few images in the beginning and then load those images in above method.
PROPERLY ALLOC AND RELEASE IMAGEVIEWS SINCE THEY CAN LEAD TO MEMORY LEAKS
Hope this works for you :)
Cheers :)
Couldn't you just use a UITableView for this?
Edit: Might be worth looking at
u can try making ur tableview horizontal by using CAAffinneTransform and giving the angle as (3.14/2) ie 90 degree annd can have a horizontal tableview with horizontal scroll and can use the above refrence given by MIKE A
regards...

iPhone Memory Warning Problems with UIScrollView

I am working on an iPad app that lets the user scroll through a number of images (usually around 20 .jpg files in a single horizontal scroll view). The file sizes are around 200 kb each. The images are set as the "Background" property of a UIButton because I wanted to allow the user to click the button to save the image. Every time I run the app, it crashes after scrolling for a little while. If you don't scroll, it behaves normal. I did run the app using the performance tools and didn't find any leaks.
I am getting a "Level=1" memory warning just before the app crashes. I had it running with about 8 images in the scrollview and never had a problem. So I know it has something to do with the scrollView and the images/buttons. In the "viewDidLoad" method, I simply did the following to set up the scrollView and then linked it in Interface Builder:
[scrollView setContentSize:CGSizeMake(5722, 340)];
[scrollView setScrollEnabled:YES];
[scrollView setDelaysContentTouches:YES];
Any ideas on how to get this to work? I added the images directly to the button in Interface Builder. Would it help if I deleted the background image, and instead set the backgrounds in the "viewDidLoad" method? Any help would be appreciated.
Sounds like what you need to do is create individual cells that these images live on, that get recycled like you'd get in a tableview. I have some code which does this that you may use for your purpose, or at least give it a go anyway. Works just like a tableview.
It's available here. This does the heavy lifting for you already, in that all you need to do is create one of these, slap it in a view somewhere, and create your individual cells. Those cells will be reused as you scroll so you won't need more than N + 2 cells loaded into memory at any one time, where N is the number that fit on your screen.
as jer wrote, the problem seems to be in the recycling. Make sure to remove, release and update the content of each cell every time you get a new one in tableView:cellForRowAtIndexPath:
(I would have commented jer's post but I'm unable to do this, probably because of my limited permissions).

Lazy loading of subViews into a non-paging UIScrollView

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

how to add more than 100 uiimageviews in uiscrollview

i have an uiscrollview and i add one by one uiimageviews but when i add more than 40 objects i have problem with memory i guess and the app crashes...what should i do? i am trying to make an app like photo viewer from apple! Help please!
i do not want thumbnais i just want to show the next image when the user flick from one to another but i have to unload the previous image and show the next one
i remove the previous like this
UIImageView l;
l=[[scroll subviews] objectAtIndex:0];
[l removeFromSuperview];
l=nil;
and then i add the next one like this
[scroll insertSubview:imageView atIndex:counter];
but i see a black background no image
please help!
The best way to do this is to load a few of the images at a time into a table view. In each cell of the table, put three thumbnails. You'll need to make a custom cell. That way, the table cells will be de-queued and the memory re-used. Check the Facebook Three20 project, I think they've implemented it like this, so you'll have some code to work with.
http://joehewitt.com/post/the-three20-project/
Ask yourself, do you really need to load all 100 images into memory? Why not just load a few images at a time in the background, depending on what image the user has scrolled to?
Don't do it that way.
If you want to display a 100 small thumbnails, resize them first with core graphics. Then they take up much less memory when you display 100 images at once.
If you want to display a 100 large images but only one is visible at a time, have 1 or 2 image views that load up the current and next images, and animate them in a clever way to make it look an endless stream. You can still use a scrollView, just monitor it's position and position your image views appropriately.
There is exactly an apple sample code that do what you want. Look for the PageControl sample, it is already implemented. Basically the slider gets the image controller from an array of controllers; among other things, when the scroller changes to the previous or next image, controllers are added and removed from this array dynamically to keep memory footprint low.
Have a look at the sample code, it is quite simple.
Hope it helps.