uiscrollview - iOS Delay even when using dispatch -
i have wallpaper app has paged scrollview on main view. each page of scrollview displays 9 images. when view scrolled i'm loading next 10 pages of images , set previous 10 pages uiimages i've loaded nil prevent memory warnings.
the problem when view scrolls , following method of scrollview gets called, there few seconds delay before view can scroll though have put block code loads new images in dispatch_async. , when comment out whole section of code dispatch stuff, there no delay.
if has idea on why happens. please please let me know.
thank much
- (void)scrollviewdidscroll:(uiscrollview *)scrollv { float fractionalpage = scrollview.contentoffset.x / self.view.frame.size.width; if (curpagenum != lround(fractionalpage)) { curpagenum = lround(fractionalpage); //display 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 9 image in each lastloadedpagenum = min(lastloadedpagenum + 10, numpages - 1); dispatch_queue_t getimages = dispatch_queue_create("getting images", nil); dispatch_async(getimages, ^{ (int = curpagenum + 4 ; <= lastloadedpagenum ; i++) { int numpicsperpage = 9; if (picsnames.count%9 && == numpages-1) { numpicsperpage = picsnames.count%9; } (int j = 0 ; j < numpicsperpage ; j++) { uiimage *image = [brain imagewith:[picsnames objectatindex:(i*9) + j]]; dispatch_async(dispatch_get_main_queue(), ^{ //loading image imageview uiimageview *imageview = (uiimageview *)[scrollview viewwithtag:image_views_tag + (i*9) + j]; imageview.image = image; }); } } }); //setting old imageview images nil release memory int oldfirstloadedpagenum = firtloadedpagenum; firtloadedpagenum = max(curpagenum - 4, 0); (int = oldfirstloadedpagenum ; < firtloadedpagenum ; i++) { int numpicsperpage = 9; if (picsnames.count%9 && == numpages-1) { numpicsperpage = picsnames.count%9; } (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; }
clearly code looping causes delay. think since dispatch inside loop gets called after bit of iterations there no real gain in using multi threading here.
Comments
Post a Comment