views:

450

answers:

1

I have a UIView with three UIScrollViews. I load each with custom UIImageViews that already have their images assigned. The custom UIImageViews are simple and only introduce a few new properties that allow for tracking the imageviews within the scrollviews.

Images load from disk very fast. The code below executes very fast but I have to wait about six seconds per imageview before anything will appear in a scrollview. If I try to load all images, the app will crash with

Program received signal:  “0”.

Usually it is an out of memory error. The photos are coming from the phone's photo library. Looking at the images used in the simulator, they range from 213KB to 401KB. I'm obviously doing something wrong but not sure what.

I could probably use a tableview since it does allow paging but I'm not sure if a tableview will have any better performance. I imagine the scrollview issue is that all imageviews must be loaded at once in a scrollview rather than lazily. Since the imageviews are already loaded into a (singleton) dictionary, why does it take so long to load them into a scrollview? All of the UI was done in IB.

In general, is there a better approach?

Here is the code that loads the imageviews into scrollviews:

- (void) addImageViewsToScrollView:(NSMutableDictionary*)imageViews scrollView:(UIScrollView*)scrollView
{

int widthRunningTotal = 0;
int count = 0;

for(NSString *s in [imageViews allKeys]){
    CustomImageView *myImageView = [imageViews valueForKey:s];
    myImageView.imageName = s;
    myImageView.imageId = count;
    myImageView.frame = CGRectMake(widthRunningTotal, 0, 160, 100);
    myImageView.contentMode = UIViewContentModeScaleAspectFit;
    [scrollView addSubview:myImageView];
    widthRunningTotal += 160;
    count++;
}
scrollView.contentSize = CGSizeMake(widthRunningTotal, 100);
}
A: 

How often does this code get called? I don't see anywhere where you remove existing subviews, so if this happens multiple times, you'll end up with a lot of subviews.

Ben Gottlieb
It occurs each time this view is loaded. The user may add new images from another view so I load all imageviews again, allowing the new images to display. Should I remove imageviews upon exiting the view? However, the memory issue occurs the first time this view is loaded.
no, if you're never calling it more than once per instantiation of the view, you're okay. I'd try loading just one view, and see what that does.
Ben Gottlieb
I have 16 images for scrollview1. I've disabled loading of the other two scrollviews. 30 seconds of waiting for an image to appear and the app gets a memory warning. I scroll the first image and get another memory warning. Scrolling the second image causes a spinning animation to appear and the app crashes with "Program received signal: '0'". This is only on the device. Simulator is fast.