Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView with dynamic height leads to memory crash

I've a native iOS screen with UITableView inside that displays some article. Some cells in this table display article image, title, author, comments, etc. But there is a single cell with UIWebView inside that displays article content. This cell has dynamic height depending on the content size. Article content goes from the server as html string in JSON response and may contain images, videos and other things that supports HTML format. I can edit this string using regular expressions depending on some requirements (for example increase font size depending on app settings). Here is an image representing my UI structure: enter image description here

The problem is that once the article content is very large, UITableView cell with UIWebView inside becomes also very large in height and this leads to memory crash. In my case this crash happens only on iPhone 6 Plus. On all of the other devices including iPhone (5, 5S, 6), iPad (2, 3, 4) (and probably other devices that supports iOS 7) app works correctly. As I suspect the reason is that iPhone 6 Plus has a high resolution screen and only 1 Gb of memory. So rendering the same content with the same amount of memory as in other devices, but in larger resolution, leads to memory crash.

I've created two test applications with UI as in the image below:

enter image description here

Both apps load the same HTML content in a single UIWebView. There is no other ui or logic in both apps.

In case a) all works correctly, scroll indicator appears and only visible content are rendered. When I'm scrolling fast, I can see white space that after a moment replaces with rendered content.

In case b) UIWebView stretches to fit content size. Test app is crashes (as my real app). As I suspect in this case even invisible content rendered and that leads to memory crash.

So my question is: How can I fix this bug without scrolling inside UIWebView? Only UITableView should be scrollable

like image 215
Sergey Alpeev Avatar asked Feb 23 '15 12:02

Sergey Alpeev


1 Answers

Make your UITableViewCell reusable. i-e(UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];). one thing very important don't use [UIimage imagenamed:@"Imagename.jpg"] which leads you to a memory crash, you can use [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Imagename" ofType:@"png"]];. I hope it might help.

like image 170
M David Avatar answered Nov 16 '22 19:11

M David