Search code examples
iosuiscrollviewdelaydispatchdispatch-async

iOS Delay even when using dispatch


I have a wallpaper app that has a paged scrollview on the main view. each page of the scrollview displays 9 images. When the view is scrolled I'm loading the next 10 pages of images and set the previous 10 pages uiimages that I've loaded to nil to prevent memory warnings.

The problem is when the view scrolls and so the following method of scrollview gets called, there is few seconds delay before the view can scroll even though I have put the block code that loads new images in dispatch_async. and when I comment out the whole section of the code with dispatch stuff, there is no delay.

If anyone has any idea on why this happens. please please let me know.

Thank you all so much

- (void)scrollViewDidScroll:(UIScrollView *)scrollV
{
float fractionalPage = scrollView.contentOffset.x / self.view.frame.size.width;

if (curPageNum != lround(fractionalPage)) {

    curPageNum = lround(fractionalPage);        

    //display the page number
    pageNumLabel.text = [NSString stringWithFormat:@"%d",curPageNum+1];
    pageNumLabel.alpha = 1.0;
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dismissPageNum) userInfo:nil repeats:NO];


    //loading more pages with 9 image in each
    lastLoadedPageNum = MIN(lastLoadedPageNum + 10, numPages - 1);

    dispatch_queue_t getImages = dispatch_queue_create("Getting Images", nil);
    dispatch_async(getImages, ^{

        for (int i = curPageNum + 4 ; i <= lastLoadedPageNum ; i++) {

            int numPicsPerPage = 9;
            if (picsNames.count%9 && i == numPages-1) {

                numPicsPerPage = picsNames.count%9;
            }

            for (int j = 0 ; j < numPicsPerPage ; j++) {

                UIImage *image = [brain imageWith:[picsNames objectAtIndex:(i*9) + j]];

                dispatch_async(dispatch_get_main_queue(), ^{

                    //loading the image to imageview
                    UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:IMAGE_VIEWS_TAG + (i*9) + j];
                    imageView.image = image;
                });
            }
        }
    });

    //setting old imageview images to nil to release memory
    int oldFirstLoadedPageNum = firtLoadedPageNum;
    firtLoadedPageNum = MAX(curPageNum - 4, 0);
    for (int i = oldFirstLoadedPageNum ; i < firtLoadedPageNum ; i++) {

        int numPicsPerPage = 9;
        if (picsNames.count%9 && i == numPages-1) {

            numPicsPerPage = picsNames.count%9;
        }

        for (int j = 0 ; j < numPicsPerPage ; j++) {

            UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:IMAGE_VIEWS_TAG + (i*9) + j];
            imageView.image = nil;
            [((UIActivityIndicatorView *)[imageView viewWithTag:ACTIVITY_INDICATOR_TAG]) startAnimating];
        }            
    }
}
}

Brain method imageWith:

-(UIImage *)imageWith:(NSString *)imageName
{
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:imageName];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

if (!image && [self connected]) {

    image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", picsURL, imageName]]]];

    if (image) {

        [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
    }

}

return image;
}

Solution

  • As suggested by Mike Pollard I used

    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0) 
    

    instead of

    dispatch_queue_create("Getting Images", nil) 
    

    and it solved the issue.

    Thanks everyone.