2
votes

I'm displaying lots of images loaded directly from my app (not downloaded). My table view is slow when I scroll it the first time. It becomes smooth after all my cell has been displayed. I don't really know why.

I have an array of UIImage that I'm loading in the viewDidLoad. Then in my tableview delegate I just get the image at a given index path and set it to an UIImageView of my cell.

Do you know how I can improve performances ?

3

3 Answers

6
votes

just to share I have fixed and it worked very well Steps I followed.

1) Set the performSelectorInBAckground function with Cell as parameter passed that holds the scroll view or uiview to put many iamges.

2) In the background function load the image stored from application bundle or local file using imagewithContents of file.

3) Set the image to the imageView using this code.

    //// Start of optimisation - for iamges to load dynamically in cell with delay , make sure you call this function in performSelectorinBackground./////

    //Setting nil if any for safety
    imageViewItem.image = nil;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^{
        UIImage *image = // Load from file or Bundle as you want

        dispatch_sync(dispatch_get_main_queue(), ^{

            //Set the image to image view not, wither cell.imageview or [cell add subviw:imageview later ]
            [imageViewItem setImage:image];
            [imageViewItem setNeedsLayout];
        });
    });

    //// End of optimisation/////

This will load all images dynamically and also scroll the table view quite smoothly than previous slow and jerky behaviour.

All the best

1
votes

You can read the answer I have just submitted here:

Loading image from CoreData at cellForRowAtIndexPath slows down scrolling

The basic idea is to use Grand Central Despatch to move your table-view-image-getting code to a separate thread, filling in your cells back on the main thread as the images become available. Your scrolling will be super-smooth even if there's a delay loading the images into memory from the filesystem.

0
votes

What I understand from your question is that your images are all set to go and that they are loaded into RAM (stored in an array which is populated in viewDidLoad). In this case, I would want to see the exact code in cellForRowAtIndexPath in order to help out. My instinct tells me that something is being done there that shouldn't be done on the main thread (as He Was suggests). The thing is - if it's only a fetch from an NSArray (worst case O(log(n))), you shouldn't be seeing a performance hit.

I know you're not downloading the images but I would still recommend to do ANY non-UI operation on a background thread. I wrote something that might help you out.