ios - Using dispatch_async to load images in UICollectionView -


i'm using parse store images. trying load images asynchronously not interfere collectionview scroll. new using dispatch_async, , not sure how implement it. i've looked lazy load, thought work. doesn't, , collectionview scroll choppy. thanks

- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath {     albumimagecell *cell = (albumimagecell *) [collectionview dequeuereusablecellwithreuseidentifier:reuseidentifier forindexpath:indexpath];      if (cell == nil) {         cell = [[albumimagecell alloc]init];     }      pfobject *temp = [_dataarray objectatindex:indexpath.row];     pffile *file = [temp objectforkey:@"image"];      if (cell.hasimage == false) {         dispatch_async(imagequeue, ^{             nsdata *data = [file getdata];                 if (data) {                     dispatch_async(dispatch_get_main_queue(), ^(void){                         cell.imageview.image = [uiimage imagewithdata:data];                         cell.hasimage = true;                     });                  }         });     }      return cell; }  

because uiimage +imagewithdata method blocks main thread bit.

dispatch_async(dispatch_get_main_queue(), ^(void){     cell.imageview.image = [uiimage imagewithdata:data]; 

so these lines should following.

uiimage *image = [uiimage imagewithdata:data];  dispatch_async(dispatch_get_main_queue(), ^(void){     cell.imageview.image = image; 

besides, uiimage doesn't decode image till uiimage object shown or rendered. see https://stackoverflow.com/a/19251240/629118. thus, following code 1 of best way rid of choppy scrolling.

uiimage *image = [uiimage imagewithdata:data];  uigraphicsbeginimagecontext(cgsizemake(1,1)); cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextdrawimage(context, cgrectmake(0, 0, 1, 1), [image cgimage]); uigraphicsendimagecontext();  dispatch_async(dispatch_get_main_queue(), ^(void){     cell.imageview.image = image; 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -