Scrollview with images still crashes even lazy load?
I have a scroll view holding a few images, each one is around 100KB. I add them to uiscrollview by creating uiviewcontrollers to hold them. controllers are stored in the scrollview. I remove its view from superview and replace it by some string when the image scrolls out of visible area. I think I'm doing fine. But I still got crash after scrolling a few times(even scroll forth and back on the开发者_Go百科 same 5 images).
I noticed every controller's dealloc is called when it's removed from the scroll view, but not its viewDidUnload.
Any help is appreciated.
viewDidUnload
is called when the viewController
itself released its view. It is not called if you released the view (which means viewDidUnload
is only relevant in the memory warning situation). In order to completely delete a view and its view controller, you should remove the view from its superview, then release the view controller. Then it's normal viewDidUnload
is not called.
It's hard to understand how you implemented this, and why it went wrong. But let me suggest a few things.
The view controller's methods such as viewWillAppear
, viewDidAppear
, viewWillDisappear
, viewDidDisappear
, etc. are not useful here, because none of them can tell you the view is currently on the screen or not. The only way you can tell is to get the contentOffset
of the scroll view every time a scroll happens (by delegate methods of UIScrollViewDelegate), and check yourself if those views are on the visible region or not.
Therefore, it's not necessary to use UIViewController there. Just use views, for example UIImageView. Dynamically create an instance of the view when it's visible, or more desirably, when it's near the visible area.
I met this issue and solved now. Hope it works for someone else. :)
Below is the code example that cause the crash(line 3):
UIImage *image = [image imageNamed:@"img.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[image release]; // Need not release here!!!
...
[cellView addSubview:imageView];
[imageView release];
精彩评论