views:

266

answers:

4

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!

+1  A: 

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?

Alex Reynolds
i do not want to load 100 images into memory i want to unload the previous image that is currently loaded and add(load) the next one.remove the previous from memory and add the next one...
stefanosn
+1  A: 

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.

Squeegy
i do not want thumbnais i just want to show the next image when the user flick from one to another
stefanosn
Flyweight pattern
Nimrod
+1  A: 

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/

nevan
A: 

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.

Andy