1

I'm experimenting with a magazine kind of iPad app that I develop and I ran into a memory issue. There are multiple UIWebView-s on a UIScrollView. Every UIWebView displays an html page each from html file source. Unfortunately I couldn't find the solution to handle the web views to load them smoothly in the background once the app loaded, instead of that it loads and fires every html pages with all its javascripts and images, because I want them to be visible once I swipe through on them, hovewer I get "Received memory warning" message at 30 panels and the app breaks.

The code I use to display the web views is this:

NSInteger numberOfViews = 47;
for (int i = 0; i < numberOfViews; i++) {
    CGFloat yOrigin = i * 768;
    DetailViewController *detailViewController2 = [[DetailViewController alloc] initWithNibName:@"Detail" bundle:nil];

    [detailViewController2.view setFrame:CGRectMake(yOrigin, 0, 768, 1024)];
    detailViewController2.view.clipsToBounds = YES;
    [scroll.view insertSubview:detailViewController2.view atIndex:0];        
}

scroll.scrollView.contentSize = CGSizeMake(768 * numberOfViews,1004);
scroll.pageControl.numberOfPages = numberOfViews;

self.view.clipsToBounds = YES;
self.view.clearsContextBeforeDrawing = YES;
CGRect frame;
frame.origin.x = scroll.scrollView.frame.size.width * scroll.pageControl.currentPage;
frame.origin.y = 0;
frame.size = scroll.scrollView.frame.size;
[scroll.scrollView scrollRectToVisible:frame animated:NO];
self.scrollViewController = scroll;
[self.view insertSubview:scroll.view atIndex:0];

- (void)dealloc {
    [scrollViewController release];
    [tableOfContentsViewController release];

    [portrait release];
    [tableOfContents1 release];
    [start release];

    [super dealloc];
}

The code that displays the html content is inside the DetaildViewController:

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:htmlfilename 
                                                         ofType:@"html" 
                                                    inDirectory:@"/htdocs" ];

    NSString *html = [NSString stringWithContentsOfFile:htmlPath 
                                               encoding:NSUTF8StringEncoding 
                                                  error:nil];

    web.delegate = self;
    [web setMultipleTouchEnabled:YES];
    [web loadHTMLString:html 
                baseURL:[NSURL fileURLWithPath:
                         [NSString stringWithFormat:@"%@/htdocs/", 
                          [[NSBundle mainBundle] bundlePath]]]];

- (void)dealloc {
    //---release the memory used by the property---
    [page release];


    [web setDelegate:nil];
    [web stopLoading];
    [web release];


    [orientation release];
    [page release];

    [super dealloc];
}

The question is how I can load them smoothly in the backround on the UIScrollView and only fire the javascripts once the panel bounces into the viewport, I plan to display more than 60 webviews with its html pages?

Heaven 7
  • 123
  • 1
  • 7

1 Answers1

0

You need to recycle your views. Creating a bunch of views when only one at a time will ever be visible is not a good strategy. Apple has a few lengthy video tutorials on the subject. The basic idea is to use at most 3 or 4 views (UiWebViews). As you swipe and the view bounds become visible you initialize it. When it's no longer visible you destroy it. The Wall Street Journal iPad & iPhone app uses a similar pattern. Here's link to a few resources & tutorial: Are there any good UIScrollView Tutorials on the net?

Community
  • 1
  • 1
smaura777
  • 326
  • 1
  • 3
  • 10