views:

154

answers:

0

Hi All,

I'm using code based on Apple's "Scrolling" sample code - here's where I have a problem:

// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= jNumImages; i++)
{
    NSString *imageName = [NSString stringWithFormat:@"page%d.png", i];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image];

The UIImageView causes a leak - it doesn't seem to be releasing (though I do state [imageView2 release]; after adding the imageView as a subView to scrollView2 I have say 15 chapters of a book each held in a nav bar, each containing a scroll view with one chapters worth of these image views (each image is a page).

When I get to around the second last chapter the app crashes due to the memory leaks... really annoying! I think it might be because imageView's been alloc'd twice (in alloc and in addSubView) but i'm not sure.... tried releasing twice but it didn't seem to help. any pointers?

Thanks in advance ^.^

update: here's the whole of the viewDidLoad void

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor blackColor];

    // 1. setup the scrollview for multiple images and add it to the view controller
    //
    // note: the following can be done in Interface Builder, but we show this in code for clarity
//  [scrollView1 setBackgroundColor:[UIColor blackColor]];
    [scrollView1 setCanCancelContentTouches:NO];
    scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView1.clipsToBounds = YES;        // default is NO, we want to restrict drawing within our scrollview
    scrollView1.scrollEnabled = YES;
    scrollView1.pagingEnabled = YES;
    scrollView1.bounces = YES;
    scrollView1.directionalLockEnabled = YES;
    scrollView1.maximumZoomScale = 1.0;
    scrollView1.minimumZoomScale = 1.0;
    scrollView1.multipleTouchEnabled = YES;
    scrollView1.delegate = self;



    // load all the images from our bundle and add them to the scroll view
    NSUInteger i;
    for (i = 1; i <= kNumImages; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
        CGRect rect = imageView.frame;
        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;
        imageView.frame = rect;
        imageView.tag = i;  // tag our images for later use when we place them in serial fashion
        [scrollView1 addSubview:imageView];
        [imageView release];


    }
[self layoutScrollImages];
}