ios - Memory leaks when loading images to scroller -
i have scrollview in load images net .i memory warnings, assume because doing wrong images loader. trying fix little things, , wanted show code here, , hear maybe there more things can fix rid of warnings.
so every time scroller (ipad) has 4/5 images : current page-3->current page+3
.
this how load images(every image has blur effect apple's classes) : (should allocated imageview every time? can improve here? )
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_background, 0), ^ { nsdata *imdata2 = [nsdata datawithcontentsofurl:url]; dispatch_async(dispatch_get_main_queue(), ^ { uiimage *theimage=[uiimage imagewithdata:imdata2 scale:1]; uiimage *lightimage = [theimage applylighteffect]; uiimage *scaledimage =[resizer resizeimagetowidth:[globals sharedglobals].imageswidth withimage:theimage]; cgrect viewsizeback=cgrectmake(scroller.bounds.size.width*topage , 0, scroller.bounds.size.width, scroller.bounds.size.height); int x=[globals sharedglobals].pagemargins; int y=([uiscreen mainscreen].bounds.size.height-scaledimage.size.height)/2; cgrect viewsizefront=cgrectmake(x , y, scaledimage.size.width,scaledimage.size.height); uiimageview *backimageview=[[uiimageview alloc] initwithframe:viewsizeback]; uiimageview *frontimageview=[[uiimageview alloc] initwithframe:viewsizefront]; backimageview.layer.cornerradius = 0.0; backimageview.layer.maskstobounds = yes; backimageview.image=lightimage; frontimageview.layer.cornerradius = 0.0; frontimageview.layer.maskstobounds = yes; frontimageview.image=scaledimage; frontimageview.layer.borderwidth=1.0; frontimageview.layer.bordercolor=[uicolor colorwithred:255.0 green:255.0 blue:255.0 alpha:1.0].cgcolor; [backimageview addsubview:frontimageview]; backimageview.tag=topage; frontimageview.tag=topage; [scroller addsubview:backimageview]; }); });
you should ever have 3 images loaded @ maximum - previous page (if exists), current page , next page.
any other images have loaded above wasteful because can't see them , they're taking memory no reason. if images aren't big can maintain them in memory , purge them when warning, large images still cause issues.
Comments
Post a Comment