0

In continuation to my question asked here, i want to know if the method to update the UI in viewDidScroll is correct. As i can observe the method is called many times and probably may be the reason why the ui become jittery and unresponsive. How can i increase the responsiveness of the UI. The code is as below:

BOOL isScrollingDown = verticalScrollView.contentOffset.y > _previousContentOffsetY;

_previousContentOffsetY = verticalScrollView.contentOffset.y;

CGFloat pageHeight = verticalScrollView.frame.size.height;

int scrollingToPageNum = isScrollingDown ? (ceil((verticalScrollView.contentOffset.y - pageHeight) / pageHeight) + 1) : (floor((verticalScrollView.contentOffset.y - pageHeight) / pageHeight) + 1);
int page = floor((verticalScrollView.contentOffset.y - pageHeight / 2) / pageHeight) + 1;

[self loadPage:(page-1)];
[self loadPage:(page)];
[self loadPage:(page+1)];

/* Unloading the pages not seen in the view is done here*/
if (!(isScrollingDown) && scrollingToPageNum >1) {
    [self unloadPages:page-2];
}else {
    [self unloadPages:page+2];
}

The lazy loading technique works fine and lot of memory usage is reduced due to it. However the UI is too slow. Also i want to know how to update the UI from a different thread which downloads image from a URL.

TIA, Praveen

Community
  • 1
  • 1
Praveen S
  • 10,355
  • 2
  • 43
  • 69

1 Answers1

0

I'm not 100% sure about this so double-check. You have to update the screen in the main thread of the application if you using Apple's classes like UIScrollView. However, you don't need to download the images in the same thread. Read Apple's Concurrency Guide and look at there sample program to figure out how.

In addition it takes longer to draw images that are larger than they need to be. Don't draw images with more pixel than there is space in the view. For super large images cut them apart so you only draw sections at a time, Apple has sample code for this. When you detect down time draw some images immediately around what's being displayed to the user. So the user doesn't see a loading symbol when scrolling around the view. There only so much the pre iPhone 3GS can handle with each iOS release the phone has gotten slower and slower, because it's doing more stuff you have to manage speed versus fidelity on those phones, drawing a low fidelity image if the user to rapidly moving around then drawing a higher fidelity image if the user stops moving around.

I'll update this later if I have the time with some links, but those tips above are what worked for me.

Tobias
  • 4,397
  • 3
  • 26
  • 33
  • I use the NSURLConnection to asynchronously download the images. And delegate method updates the UI. If image is already downloaded then the image view is directly updated with the data. The images are thumbnails of videos from youtube. I am not able to figure out the jitterniess of the UI when i scroll it. I want the scroll to be fully free flowing and not slow. Feel free to ask me any questions you may want to know about my implementation. – Praveen S Feb 23 '11 at 18:22
  • Questions: What device are you using? What iOS version is on it? What version of the SDK are you using? What are the dimensions of the images you are downloading? What are the formats of the images? How many pixels are you drawing each image to? Can I download and look at your project I might be able to spot something else going on? – Tobias Mar 01 '11 at 21:27
  • @Gaspy1909 - Sorry about delayed reply. I am using ipod 2g so that it works better on newer devices. It has the latest ios 4.2 on it. SDK is also the latest from app dev site. Each images is about 150x95 in size which i place in a uiimageview as part of uiview. – Praveen S Mar 07 '11 at 05:42
  • @Gaspy1909 - I cant give you the source code for security reasons but i have pasted relevant questions in my other questions. http://stackoverflow.com/questions/5133731/how-to-make-ui-responsive-all-the-time-and-do-background-updating and http://stackoverflow.com/questions/5190715/crash-in-background-thread-in-uiscrollview. – Praveen S Mar 07 '11 at 05:43
  • Okay. Should have read you're other questions. You have 8 images for each page. I suggest you programmatically constructing a single composite image for each page out of the 8 smaller images. So you'll only be using a single UIImageView per page. That way when scrolling from one page to another the SDK is only doing calculations for 2 UIImageViews instead of 16. Hopefully that will speed up your performance some what. – Tobias Mar 09 '11 at 08:23
  • @Gaspy1909 - Hey i tried using the AQgridview suggested by some for grid view implementations. Looks fine for now. However i got some good hints from your reply. So will accept that. – Praveen S Mar 13 '11 at 16:11
  • Thanks for accepting. I'm glad you found a solution. Sorry I wasn't of more help. I'll check out AQGridView myself, because I've had similar issues and they were nasty to code around. On an iPhone 3G (not 3GS) the app still feels a bit slow. – Tobias Mar 14 '11 at 17:24