Reputation: 469
I'm developing an iPad app. I have some UIViewController (viewController) with UIScrollView (scrollView) with paging enabled. I add some child UIViewControllers (childVC_1, childVC_2) to scrollView.
Each of childVC takes almost the entire screen and have 5–10 large images as subviews. I can't load all needed childVCs and store them in memory, so I add only first childVC. I want to dynamically add next childVCs and remove previous, to keep memory clean. I have asynchronously preloaded all images needed for next childVC, and store them in some NSArray (images). All images are rendered.
- (UIImage *)renderedImageFromImage:(UIImage *)image
{
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
[image drawInRect:rect];
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return renderedImage;
}
I wand to add next childVC when user start to scroll. I initialize childVC with preloaded images. Adding child view controllers is very common.
childVC = [[ChildVC alloc] initWithImages:images];
[self addChildViewController:childVC];
[self.scrollView addSubview:childVC.view];
childVC.view.frame = CGRectMake(xCoord, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
[childVC didMoveToParentViewController:self];
I hoped it will be fast, because I have preloaded all needed images. I have some logs in childVC's viewDidLoad method, and everything is very fast. But there is a big delay between viewWillAppear and viewDidAppear (about half of second).
I don't know what really happens between this two methods and I have no idea what else I can do to make it faster. Hope somebody have some ideas.
Upvotes: 0
Views: 1204
Reputation: 86145
I suspect whether your preloading/composition code is actually done in background thread. Also 0.5 second delay is too big if you really did it in background thread. Check whether it is actual running in background thread by;
[[NSThread mainThread] isEqual:[NSThread currentThread]]
Also, your main thread may be blocked to wait for your background thread. Where are you calling the second code block? How do you synchronize the image data to main thread? If it's really done in background thread asynchronously, the execution point will be random, and can't be fixed. But it seems you're running the second code block in viewWillAppear
method.
Upvotes: 1
Reputation: 10433
Try to not add or remove views from the view hierarchy during scrolling. If you can do reuse views that are already in the scrollview, change the content and rearrange the frame. (So for example if you scroll down take a view that is not longer visible at top, change the content and move it below the current position.
From my experience the call of addSubview
and removeFromSuperView
can add small delays during scroll.
Upvotes: 1