objective c - didReceiveMemoryWarning from UITableView with Images created with url data followed by app termination due to memory pressure -


i setting uitableview, , on side of each cell image 80x80. tableview works when number of cells less 8, more , scroll performance can have minor delay. lot more cells (around 50), scroll performance fine long dont make big swipe down quickly. also, when there lot of cells, if stay on view long , scroll , down, app receive memory warning, , crash. tips helping save memory? of these images pretty large (5000 x 1000), or how clear memory when didreceivememorywarning called? i'm using arc if relevant. here code use set cells:

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {  customtableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"articlecell"];  if(cell == nil){     cell = [[customtableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@"articlecell"]; } cell.titlelabel.text = nil; cell.imageright.image = nil;  article *article = [self.articles objectatindex:indexpath.row];  [cell.titlelabel settext:article.title];  if(!article.picturedata){     dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default,0), ^{     nsdata* data = [nsdata datawithcontentsofurl:[nsurl urlwithstring:article.image]];         dispatch_sync(dispatch_get_main_queue(), ^{             article.picturedata = data;             uiimage *img = [[uiimage alloc] initwithdata:data];             cgsize imgsize = cgsizemake(80, 80);             img = [img resizeimage:img newsize:imgsize];             [cell.imageright setimage:img];             [self.tableview beginupdates];             [self.tableview reloadrowsatindexpaths:[nsarray arraywithobjects:indexpath, nil] withrowanimation:uitableviewrowanimationnone];             [self.tableview endupdates];         });     }); } else [cell.imageright setimage:[uiimage imagewithdata:article.picturedata]]; return cell; } 

the point though tableview loading of cells perfectly, , can make way through 50+ rows. need clear memory, i'm unsure how.

your cells set differently depending on whether picturedata available:

if picturedata not yet available, downloaded & full resolution data set on article object; afterwards smaller thumbnail image generated , set on cell.imageright.

however, if picture has been downloaded, full resolution image created picturedata , set on cell's imageright without resizing first..

} else     [cell.imageright setimage:[uiimage imagewithdata:article.picturedata]]; 

this means pictures have been loaded, each time tableview dequeues reusable cell set full res image on cell.imageright. suppose explains memory issues encountering.

my advice add uiimage *thumbnail property articles object, cache resized image there after has been generated downloaded data , use -thumbnail cell's imageright. additionally, reduce nontrivial system load (both on memory & cpu) caused re-generating , resizing images every time tableview updates cell.

another thing: there's no need bother main thread image resize operation, suspect that's hitching comes when scrolling. should leave on global queue , dispatch_async table update main queue afterwards.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -